如何设置 Accept 和 Accept-Language 标头字段?

发布于 2024-11-14 00:09:41 字数 648 浏览 1 评论 0原文

我可以设置 Request.Content-Type = ... , Request.Content-Length = ...

如何设置 Accept 和 Accept-Language?

我想上传文件(RFC 1867)并且需要创建如下请求:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

云醉月微眠 2024-11-21 00:09:41

查看接受属性

HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(myUri);
myHttpWebRequest.Accept="image/*";    
HttpWebResponse myHttpWebResponse=
         (HttpWebResponse)myHttpWebRequest.GetResponse();

这篇 MSDN 文章展示了如何向您的请求添加自定义标头:

//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");

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");
月牙弯弯 2024-11-21 00:09:41

当你想设置Accept类型和内容类型时,只需将webrequest强制转换为HttpwebRequest

var webreq= (HttpWebRequest)WebRequest.Create(requestUri);
webreq.Method = "POST";
webreq.Accept = "application/json";
webreq.ContentType = "application/json";

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";
以可爱出名 2024-11-21 00:09:41

您需要确保将请求类型转换为 (HttpWebRequest),其中接受标头属性可用。

在旧的 WebRequest 类中,Accept 标头不可访问。

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.

错々过的事 2024-11-21 00:09:41

在多次尝试使用标头后,我必须确认

myWebHeaderCollection.Add("foo","bar"); 解决方案完美运行。

如果你想设置语言。

myWebHeaderCollection.Add("AcceptCharset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
myWebHeaderCollection.Add("TransferEncoding", "gzip,deflate");

但不设置值。鉴于第一个可行,这似乎是一个合乎逻辑的结论。

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.

随梦而飞# 2024-11-21 00:09:41

如果您使用 HttpRequestMessage,请设置标头使用 Headers.Add 方法。在你的情况下:

request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

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");
寄意 2024-11-21 00:09:41

在我有幸维护 15 年前的 vb.NET 3.5 代码的情况下,这个解决方法对我来说是成功的:

webReq = WebRequest.Create(apiHost)
CType(webReq, HttpWebRequest).Accept = "application/json"

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文