How to set Accept and Accept-Language header fields?
Asked Answered
S

6

25

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
Species answered 3/6, 2011 at 7:57 Comment(0)
C
44

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");
Calise answered 3/6, 2011 at 8:6 Comment(4)
But now i have a problem...I send image and audio files but only image files was send.No audio file.can it be about my headers or attirubutes ?Species
@Mennan: what kind of problem?Calise
@Mennan: you'll probably have to ask another question with appropriate amount of information, it's hard to say something without proper code sample and result descriptionCalise
hmm ok understand i will do it in 10 min.Thx for your helpSpecies
S
3

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";
Slipcase answered 29/8, 2017 at 4:19 Comment(0)
T
2

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.

Tiffany answered 6/8, 2014 at 13:1 Comment(0)
A
1

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.

Angadresma answered 30/7, 2011 at 13:7 Comment(1)
Infuriatingly (because it is incomprehensible) however, Microsoft chose not only to ignore the HTTP model and invent their own "convenient" model instead (e.g. by having an Accept property on an object representing a request, when this is just an HTTP header), but to add insult to injury by not even making sure the generic methods work! Try to set "Accept" with this, and you get ArgumentException saying you can't set "restricted headers" (an artifact of their model, nothing to do with HTTP).Ardis
C
1

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");
Chirk answered 16/9, 2013 at 23:53 Comment(0)
S
0

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"
Shatter answered 18/10, 2022 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.