使用System.Net.WebRequest时,无法设置的HTTP标头 [英] Cannot set some HTTP headers when using System.Net.WebRequest

查看:646
本文介绍了使用System.Net.WebRequest时,无法设置的HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试添加一个HTTP头键/值对的WebRequest 对象上,我得到了以下异常:

When I try to add a HTTP header key/value pair on a WebRequest object, I get the following exception:

这头必须使用适当的属​​性进行修改

This header must be modified using the appropriate property

我试着使用Add()方法,但我仍然得到同样的异常增加新值标题集合。

I've tried adding new values to the Headers collection by using the Add() method but I still get the same exception.

webRequest.Headers.Add(HttpRequestHeader.Referer, "http://stackoverflow.com");

我可以通过铸造WebRequest对象来解决这个问题一个HttpWebRequest和设置属性,如 httpWebReq.Referer =htt​​p://stackoverflow.com,但这个只适用于通过属性公开头屈指可数。

I can get around this by casting the WebRequest object to a HttpWebRequest and setting the properties such as httpWebReq.Referer ="http://stackoverflow.com", but this only works for a handful of headers that are exposed via properties.

我想知道如果有一种方式来获得在与远程资源的请求修改标题一个更加精确的控制。

I'd like to know if there's a way to get a finer grained control over modifying headers with a request for a remote resource.

推荐答案

如果您需要在短期和技术去回答正确答案的最后一节。

If you need the short and technical answer go right to the last section of the answer.

如果你想知道更好,读这一切,我希望你会喜欢...

If you want to know better, read it all, and i hope you'll enjoy...

我今天也反驳这个问题,我今天发现是:

I countered this problem too today, and what i discovered today is that:


  1. 以上答案是真实的,因为:

  1. the above answers are true, as:

1.1它告诉你,你正试图添加已经存在,那么你应该修改使用相应的属性(索引,例如)它的价值,而不是试图将头重新添加。

1.1 it's telling you that the header you are trying to add already exist and you should then modify its value using the appropriate property (the indexer, for instance), instead of trying to add it again.

1.2任何时候你要改变的标题的HttpWebRequest ,您需要将对象本身上使用相应的属性,如果存在的话。

1.2 Anytime you're changing the headers of an HttpWebRequest, you need to use the appropriate properties on the object itself, if they exist.

有关和Jvenema感谢领导指引...

Thanks FOR and Jvenema for the leading guidelines...

不过,我发现了什么,和里的那个谜缺少的一块是:

  1. But, What i found out, and that was the missing piece in the puzzle is that:

2.1 WebHeaderCollection 类一般是通过的WebRequest .Headers或 WebResponse类 .Headers。的一些常见的头被认为是限制,并且被直接由API由系统暴露(例如内容类型)或受保护的,并且不能被改变。

2.1 The WebHeaderCollection class is generally accessed through WebRequest.Headers or WebResponse.Headers. Some common headers are considered restricted and are either exposed directly by the API (such as Content-Type) or protected by the system and cannot be changed.

受限制的标题是:


  • 接受

  • 连接

  • 的Content-Length

  • 内容类型

  • 日期

  • 期望

  • 主机

  • 的If-Modified-Since的

  • 范围

  • 引用站点

  • 传输编码

  • 用户代理

  • - 代理连接

  • Accept
  • Connection
  • Content-Length
  • Content-Type
  • Date
  • Expect
  • Host
  • If-Modified-Since
  • Range
  • Referer
  • Transfer-Encoding
  • User-Agent
  • Proxy-Connection

所以,下一次你面对这个异常,不知道如何解决这个问题,请记住,有一些限制头文件,解决的办法是使用合适的属性明确从的WebRequest / 的HttpWebRequest 类。

So, next time you are facing this exception and don't know how to solve this, remember that there are some restricted headers, and the solution is to modify their values using the appropriate property explicitly from the WebRequest/HttpWebRequest class.

编辑:(有用的,自评,由用户<评论href=\"http://stackoverflow.com/questions/239725/cannot-set-some-http-headers-when-using-system-net-webrequest#comment15470171_4752359\">Kaido)

(useful, from comments, comment by user Kaido)

解决方案是检查头已经存在或调用之前添加限制( WebHeaderCollection.IsRestricted(键)

Solution is to check if the header exists already or is restricted (WebHeaderCollection.IsRestricted(key)) before calling add

这篇关于使用System.Net.WebRequest时,无法设置的HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆