I can set Request.Content-Type = ... , Request.Content-Length = ...
How to set Accept and Accept-Language?
I want to upload a file (RFC 1867) and need to create a request like this:
POST /test-upload.php.xml HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: tr-tr,tr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------21724139663430
Content-Length: 56048
Take a look at Accept property:
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(myUri);
myHttpWebRequest.Accept="image/*";
HttpWebResponse myHttpWebResponse=
(HttpWebResponse)myHttpWebRequest.GetResponse();
This MSDN article shows how to add custom headers to your request:
//Get the headers associated with the request.
WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;
//Add the Accept-Language header (for Danish) in the request.
myWebHeaderCollection.Add("Accept-Language:da");
//Include English in the Accept-Langauge header.
myWebHeaderCollection.Add("Accept-Language","en;q=0.8");
When you want to set the Accept type and content type, just cast the webrequest to HttpwebRequest
var webreq= (HttpWebRequest)WebRequest.Create(requestUri);
webreq.Method = "POST";
webreq.Accept = "application/json";
webreq.ContentType = "application/json";
You need to be sure that you type cast the request to (HttpWebRequest), where the accept header property is available.
In the old WebRequest class, the Accept header is not accessible.
I have to confirm after several annoying attempts to use the headers that the
myWebHeaderCollection.Add("foo","bar"); solution works perfectly.
if you want to set the Language.
myWebHeaderCollection.Add("AcceptCharset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
myWebHeaderCollection.Add("TransferEncoding", "gzip,deflate");
Does not set the values however. Which may seem like a logical conclusion given the first one works.
If you are using HttpRequestMessage, set the header using Headers.Add method. In your case :
request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
In a case where I have the pleasure of maintaining 15 year old vb.NET 3.5 code, this workaround was successful for me:
webReq = WebRequest.Create(apiHost)
CType(webReq, HttpWebRequest).Accept = "application/json"
Related
I'm using RestSharp to call an external API.
This works:
var client = new RestClient(apiUrl);
var request = new RestRequest(myurl, Method.GET);
foreach (var param in parameters)
{
request.AddQueryParameter(param.Key, param.Value);
}
var response = client.Execute(request);
This doesn't:
var client = new RestClient(apiUrl);
var request = new RestRequest(myurl, Method.GET);
foreach (var param in parameters)
{
request.AddParameter(param.Key, param.Value);
}
var response = client.Execute(request);
Resulting in:
System.Exception: API Call MyWebAPIMethod GET: Failed with status code
0 - Unable to connect to the remote server
What's the difference between AddParameter and AddQueryParameter?
According to the documentation they should function the same when using HttpGET and according to Fiddler they seem to generate the same URL as well.
To answer your question
AddQueryParameter adds a parameter in the query string as ParameterType.QueryString whereas AddParameter(string, object) adds the parameter as ParameterType.GetOrPost
For more details on each parameter type, see:
GetOrPost: https://github.com/restsharp/RestSharp/wiki/ParameterTypes-for-RestRequest#getorpost
QueryString: https://github.com/restsharp/RestSharp/wiki/ParameterTypes-for-RestRequest#querystring
To solve your problem
It seems it is unrelated to the type of parameter, because the exception thrown seems to indicate you aren't even connecting to the remote server.
make sure you pass the same apiUrl / myUrl in both cases.
To answer the OP and anyone else who might have trouble with the concept.
It took me awhile to get around to the concept.
You probably need to visualise the RESTful standard of how to construct a GET request message in a url against constructing for a POST request message.
You will notice that for GET , the parameter(s) are attached to the URL header
whereas for the POST , the parameter(s) are placed in the body of the message.
RestSharp 's method AddQueryParameter() will only add the (Query) parameters in the header of the message, whereas the AddParameter() will only add the parameters to the mesasge body. As demonstrated below the GET has one query parameter with a value of "Flavours" . For the POST , the parameters contact_name and company_name are located in the bottom of the message body.
Eg:
GET message format :
GET http://www.consumerdiarydemo.cbrnetwork.test.au/api/ConsumerDiary/getSizesOrFlavours/Flavours HTTP/1.1
Host: www.consumerdiarydemo.cbrnetwork.test.au
Connection: keep-alive
Accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Referer: http://www.consumerdiarydemo.cbrnetwork.test.au/ConsumerDiaryPage2template
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
POST message format :
POST
http://localhost:1234567/api/customers HTTP/1.1
Accept: application/json, text/javascript, /; q=0.01
X-Requested-With: XMLHttpRequest
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
{"contact_name":"value_data1","company_name":"value_data2"}
AddParameter / Get or Post
GetOrPost behaves differently based on the method. If you execute a GET call, RestSharp will append the parameters to the Url in the form url?name1=value1&name2=value2.
On a POST or PUT Requests, it depends on whether you have files attached to a Request. If not, the Parameters will be sent as the body of the request in the form name1=value1&name2=value2.
Ref: https://restsharp.dev/usage.html#get-or-post
AddQueryParameter / Query String
QueryString works like GetOrPost, except that it always appends the parameters to the url in the form url?name1=value1&name2=value2, regardless of the request method.
Ref: https://restsharp.dev/usage.html#query-string
This is a follow-up question to the suggestion of user #Kiewic to make a post request using a webview: Post data with a request in a Windows Store app WebView - using C#
So I am using this code
// defined before: string uri, User user
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(uri));
request.Content = new HttpStringContent(
String.Format(
"language={0}&session_id={1}&user_id={2}",
Text.GetLanguage(),
user.session_id,
user.user_id.ToString()
),
Windows.Storage.Streams.UnicodeEncoding.Utf8,
"application/x-www-form-urlencoded"
);
webView.NavigateWithHttpRequestMessage(request); // webView defined in xaml
Fiddler shows me this request:
POST http://mobile.mysite.com/ HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US,en;q=0.8,de-CH;q=0.5,de;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; SMJB; WebView/2.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: mobile.mysyte.com
Content-Length: 101
Connection: Keep-Alive
Pragma: no-cache
Cookie: _ga=GA1.2.769009833.1387593479; PHPSESSID=nk6b04rb7d7vu4vmm2ish7l0o4
language=en&session_id=fhihferihguiegierewfrefghxrfer&user_id=1
The webserver technology is PHP, so I used this code to print the post content
<?php
var_dump($_POST);
?>
But the result is an empty array. So where is my mistake?
According to a Microsoft employee there is a bug with setting the headers of the request content. This should mean that nothing is wrong with my code in the first post:
http://social.msdn.microsoft.com/Forums/windowsapps/en-US/7deaf4ba-611f-4957-af3c-9b2b2e1e1b8b/webviewnavigatewithhttprequestmessage-set-contenttype?forum=winappswithcsharp
This means you cannot really use NavigateWithHttpRequest and you should use another approach to post data. User #Kiewic made a suggestion which works on Windows 8 too:
Post data with a request in a Windows Store app WebView - using C#
I'm trying to send the same information from my application as I send from the browser. Here is part of data captured by Fiddler:
POST http://something/ HTTP/1.1
Host: something.com
Connection: keep-alive
I got stuck with this connection property. If I set the property keep-alive to true, in Fiddler I see this:
Proxy-Connection: Keep-Alive
If I try to set the connection property to Keep-alive, I get this error:
Keep-Alive and Close may not be set using this property.
How to write the code so that in Fiddler I can see this:
Connection: keep-alive
My full code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myUrl ");
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.Accept = "*/*";
WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("Accept-Encoding", "myEncoding");
headers.Add("Accept-Language", "myLang");
request.Headers = headers;
request.ContentType = "myContentType";
request.Referer = "myReferer";
request.UserAgent = "myUserAgent";
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "myData";
byte[] data = encoding.GetBytes(postData);
request.GetResponse().Close();
To have your application send a Connection: Keep-Alive header, use the KeepAlive property on the HttpWebRequest object.
When a client knows that it is behind a proxy (like Fiddler), it may send a Proxy-Connection: Keep-Alive header instead of a Connection: Keep-Alive header. The expectation is that a HTTP/1.1 proxy (like Fiddler) will convert that header from Proxy-Connection to Connection before passing it to the upstream server.
This "proxy renames header" pattern was introduced many years ago to attempt to workaround hangs in HTTP/1.0 servers that didn't support Keep-Alive properly; the idea is that the server would ignore the Proxy-Connection header if the outdated proxy didn't rename the header by removing the Proxy- prefix.
Trying To Loading Html Content Of "http://links.casemakerlegal.com/states/CA/books/Case_Law/results?search[Cite]=214 Cal.App.3d 533" but HttpWebResponse object Giving This Error "(500) Internal Server Error"
And Code Is------
request = WebRequest.Create(urlCheck); request.Timeout = 100000; response = request.GetResponse(); strmRead = new StreamReader(response.GetResponseStream(),System.Text.Encoding.UTF8); result = strmRead.ReadToEnd();
You need to use a tool like Wireshark or Ethereal, or the developer tools in your browser to investigate this further. It is likely the browser is sending some values in the HTTP Header that your code is not, and the server is returning a 500 due to these missing values. Try replicating all of the headers that the browser is using in your code to see if this resolves the problem.
It is usually browser agent. try adding a valid browser agent to your request headers along with Accept and Accept-Encoding headers,
*Edit: For example:
request.UserAgent = "Mozilla/5.0 (Windows NT 5.2; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
request.Headers.Add("Accept-Encoding: gzip, deflate");
request.Headers.Add("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
request.Headers.Add("Accept-Language: en;q=0.8");
I'm trying to send to send the following header with my HttpWebRequest:
Connection: keep-alive
However, the header is never sent. Fiddler2 is showing that whenever I request the page in Google Chrome, the header is sent. However, my application refuses to send this header for some reason.
I have set the KeepAlive property to true (it's true by default anyway), yet the header still does not get sent.
I am trying to send this header with multiple HttpWebRequests, but they all basically look like this:
HttpWebRequest logIn6 = (HttpWebRequest)WebRequest.Create(new Uri(responseFromLogIn5));
logIn6.CookieContainer = cookies;
logIn6.KeepAlive = true;
logIn6.Referer = "https://login.yahoo.com/config/login?.src=spt&.intl=us&.lang=en-US&.done=http://football.fantasysports.yahoo.com/";
logIn6.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1";
logIn6.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
logIn6.Headers.Add("Accept-Encoding:gzip,deflate,sdch");
logIn6.Headers.Add("Accept-Language:en-US,en;q=0.8");
logIn6.Headers.Add("Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3");
logIn6.AllowAutoRedirect = false;
HttpWebResponse logIn6Response = (HttpWebResponse)logIn6.GetResponse();
string responseFromLogIn6 = logIn6Response.GetResponseHeader("Location");
cookies.Add(logIn6Response.Cookies);
logIn6Response.Close();
Does anyone know what I have to do to make sure this header is sent?
Fiddler2 Raw From Chrome:
GET xxx HTTP/1.1
Host: accounts.google.com
Connection: keep-alive
Referer: https://login.yahoo.com/config/login?.src=spt&.intl=us&.lang=en-US&.done=http://football.fantasysports.yahoo.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: xxx
HTTP/1.1 302 Moved Temporarily
Set-Cookie: xxx
Set-Cookie: xxx
Location: xxx
Content-Type: text/html; charset=UTF-8
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Date: Sat, 17 Sep 2011 22:27:09 GMT
Expires: Sat, 17 Sep 2011 22:27:09 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Content-Length: 2176
Server: GSE
Fiddler2 Raw From My Application:
GET xxx HTTP/1.1
Referer: https://login.yahoo.com/config/login?.src=spt&.intl=us&.lang=en-US&.done=http://football.fantasysports.yahoo.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Host: accounts.google.com
HTTP/1.1 302 Moved Temporarily
Location: xxx
Content-Type: text/html; charset=UTF-8
Date: Sun, 18 Sep 2011 00:05:40 GMT
Expires: Sun, 18 Sep 2011 00:05:40 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Content-Length: 573
Server: GSE
I'm trying to get the second Fiddler2 raw information to look like the first Fiddler2 raw information.
I've had the same issue: The Connection: Keep-Alive header is not sent except the first request, and the server I accessed won't give me the correct response if it is missing. So, here are my workarounds to this issue:
First is set the ProtocolVersion property of HttpWebRequest instance to HttpVersion.Version10. Except the http command will become GET xxx HTTP/1.0, it works and uses only the public API.
The second way uses the reflection to modify the internal property ServicePoint.HttpBehaviour of HttpWebRequest instance, like this:
var req = (HttpWebRequest)WebRequest.Create(someUrl);
var sp = req.ServicePoint;
var prop = sp.GetType().GetProperty("HttpBehaviour",
BindingFlags.Instance | BindingFlags.NonPublic);
prop.SetValue(sp, (byte)0, null);
req.GetResponse().Close();
Hope this helps.
I struggled with this problem for half a day! And dear old Fiddler (my guardian angel) was inadvertantly part of the problem:
Whenever I tested my HTTP POSTs using Fiddler monitoring ON - the problem DIDN'T appear
Whenever I tested my HTTP POSTs with Fiddler monitoring OFF - the problem DID appear
My POSTS were sent with protocol 1.1 and the Keep-Alive was ignored/redundant/why after the initial connection. i.e. I could see it in the header of the first POST (via Fiddler!), but not in subsequent POSTs despite using the same code. Hey ho ...
But the remote server would only respond if Keep-Alive was sent. Now I can't prove this, but I suspect that Fiddler monitoring the connection caused the remote server to think or believe that the connection was still active (despite no Keep-Alives sent after my first POST) and responded correctly. As I said, the second I turned Fiddler off, the absence of Keep-Alives caused the remote server to timeout on me..
I implemented the 1.0 solution described above and my POSTS now work, with or without Fiddler on or off. Hope this helps somebody else stuck somewhere ...
You doing it right. The code should result in following header added:
Connection: Keep-Alive
Post the code that you use for sending request and Raw output from Fiddler if you don't see this header. You may also ignore this because HTTP 1.1 connection is keep-alive by default.
Update: it looks like .NET only sets Keep-Alive explicitly for the first (!) request. Further requests to the same host/url will not have this header presumably because underlying tcp connection is already being reused.
After downloading HttpWebRequest source code, noticed that every property checks some known headers for HeaderCollection. To get rid of that doing some reflection stuff on that collection make it work
var webRequest = (HttpWebRequest) WebRequest.Create(url);
webRequest.Headers.GetType().InvokeMember("ChangeInternal",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
Type.DefaultBinder, webRequest.Headers, new object[] {name, value}
);
I know the answer for this as I had the same problem and managed to solve it by inheriting the webclient and overriding it's Get Web Request method.
See the code below:
public class CookieAwareWebClient : WebClient
{
public CookieContainer CookieContainer { get; set; }
public CookieAwareWebClient()
: this(new CookieContainer())
{ }
public CookieAwareWebClient(CookieContainer c)
{
this.CookieContainer = c;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var castRequest = request as HttpWebRequest;
if (castRequest != null)
{
castRequest.KeepAlive = true; //<-- this what you want! The rest you don't need.
castRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
castRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36";
castRequest.Referer = "https://www.jobserve.com/gb/en/Candidate/Login.aspx?url=48BB4C724EA6A1F2CADF4243A0D73C13225717A29AE8DAD6913D";
castRequest.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
castRequest.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6");
castRequest.CookieContainer = this.CookieContainer;
}
return request;
}
}
As you can see I am not only enabling keep-alive but I am utilizing cookies and other headers also!
I hope that helps!
Kiran