Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Accept and Accept-Language header fields?

Tags:

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 
like image 916
Mennan Avatar asked Jun 03 '11 07:06

Mennan


People also ask

How do I accept a language header?

To check this Accept-Language in action go to Inspect Element -> Network check the request header for Accept-Language like below, Accept-Language is highlighted you can see. Supported Browsers: The browsers are compatible with HTTP Accept-Language header are listed below: Google Chrome.

How do I add a accept Encoding header?

To check this Accept-Encoding in action go to Inspect Element -> Network check the request header for Accept-Encoding like below, Accept-Encoding is highlighted you can see.

How do I set accept-language?

The user can change the Accept-Language header sent by the browser using the browser's preference settings. E.g., in Chrome, go to “Settings”, click on “Show advanced settings...”, scroll down to “Languages”, click on “Language and input settings...”, and then add languages and drag to order them.

Is accept-language standard HTTP header?

The Accept-Language request HTTP header indicates the natural language and locale that the client prefers. The server uses content negotiation to select one of the proposals and informs the client of the choice with the Content-Language response header.


2 Answers

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"); 
like image 72
Dyppl Avatar answered Oct 06 '22 02:10

Dyppl


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"; 
like image 42
Adi_Pithwa Avatar answered Oct 06 '22 02:10

Adi_Pithwa