How can I set the Referer of a WebRequest - windows-runtime

I tried the following:
HttpWebRequest request = ...
request.Headears["Referer"] = "http:.....";
But I get the following error:
The 'Referer' header must be modified using the appropriate property or method.
What is the approriate property or method?
HttpWebRequest does not have a Referer property.

Related

Passing JSON to a WCF web method

I have a web service where some clients are experiencing issues calling the app.
The issue relates to them passing in JSON, and specifying the Content-Type header as JSON, but the WCF service doesn't seem to accept that header, and either wants that header not to be set, or to be set to octet-stream.
The strange thing is, a separate web method works when the Content-Type header is set to application/json.
Here is the Attributes and declaration of the method I am having trouble with
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "events/{eventId}/devices/{deviceId}/setid?ac={ac}")]
OperationResult SetId(string eventId, string deviceId, string ac, Stream data);
And this is the attributes and declaration of the method which works
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "activate?deviceId={deviceID}")]
OperationResult<ActivationResult> ActivateDevice(string deviceID, Stream data);
Now I know they are 2 different methods, but the activate method works if I provide the Content-Type header of application/json, but the SetId method does not.
In the actual implementation of each method, the stream parameters are converted to the corresponding JSON class. Any ideas why this is not working for both?
The exception message is 'Incoming message for operation 'SetId' contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'.

Windows.Web.Http.HttpClient Authorization header without scheme.

I have a winrt app and a Windows.Web.Http.HttpClient
I want to set its Authorization header without using a scheme. My code is as below.
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Scheme", "mytoken");
This will result in this
Authorization: Scheme mytoken
What I want is this
Authorization: mytoken
The problem is that the Constuctor of HttpCredentialsHeaderValue has to take a scheme argument and that scheme cannot be String.empty
Is there a way I can achieve this result?
Try:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAppendWithoutValidation(
"Authorization",
"mytoken");

MvxRestRequest - Cannot supply Body for request

In the MvvmCross Network plugin, if you use the MvxRestRequest class, there isn't a BODY property to attach content to.
MvxRestRequest req = new MvxRestRequest("url", "POST");
'req' won't have a BODY property.
In my case, when I do a POST and don't supply a body the server returns an error of "Length Required", so the 'Content-Length' header is missing.
To get around the length issue, I tried to add this:
request.Headers.Add("Content-Length", "0");
But get an error:
The 'Content-Length' header must be modified using the appropriate property or method.
Parameter name: name
How do I POST a message with content?
There are several classes that appear to support appending content to the BODY of the request:
MvxStringRestRequest
MvxStreamRestRequest
Using these DOES provide a BODY property.
var req = new MvxStringRestRequest("url", "POST);
req.Body = "some content";

How to use post method of groovyx.net.http.RESTClient

I have a RESTClient class, I want to do a POST to a web service.
My POST request will use JSON.
So in the Groovy class, the call looks like this:
def restClient = new RESTClient(url)
def bodyContent = "{\"products\":[{\"ProductId\":1,\"ProductName\":\"Product\"}]}"
response = restClient.post(
headers:['Authorization': auth],
contentType : jsonContentType,
body: bodyContent
)
the header and contenttype string is correct.
I put a JSON data as String type in the bodyContent and put into the body of the POST request.
I run this method and get error:
at groovyx.net.http.HTTPBuilder$RequestConfigDelegate.setPropertiesFromMap(HTTPBuilder.java:1111)
at groovyx.net.http.HTTPBuilder$RequestConfigDelegate.<init>(HTTPBuilder.java:946)
at groovyx.net.http.RESTClient.post(RESTClient.java:140)
at groovyx.net.http.RESTClient$post.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
So i wonder how can i make a POST request using RESTClient?
I look into the document http://groovy.codehaus.org/modules/http-builder/doc/rest.html here. The POST example is not applicable to my situation.

Getting json using http web request using get

i'm trying to get a response from an httpwebrequest using get method and content type json..
but i'm getting Cannot send a content-body with this verb-type
here is my code:
Dim objRequest As HttpWebRequest = WebRequest.Create(url)
Dim reqBytes As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(strPost)
objRequest.Method = "GET"
objRequest.Timeout = "15000"
objRequest.ContentLength = reqBytes.Length
objRequest.ContentType = "application/json; charset=utf-8"
Try
myWriter = objRequest.GetRequestStream()
myWriter.Write(reqBytes, 0, reqBytes.Length)
Catch e As Exception
writetotext(e.toString)
End Try
am i missing something here?
HTTP GET cannot have a message body. Data gets typically passed through the URI path and query string and not through message body for GET requests. For POST, PUT, etc, you should be able to do what you are trying to do in the code above.