使用 WebClient C# 添加请求标头

发布于 2024-12-07 01:58:57 字数 947 浏览 3 评论 0原文

我有以下代码,用于将网页下载到字节数组中,然后使用 Response.Write 打印它:

WebClient client = new WebClient();

byte[] data = client.DownloadData(requestUri);

  /***********        Init response headers    ********/
  WebHeaderCollection responseHeaders = client.ResponseHeaders;
  for (int i = 0; i < responseHeaders.Count; i++)
       {
            Response.Headers.Add(responseHeaders.GetKey(i), responseHeaders[i]);
       }
  /***************************************************/

除了响应标头之外,我还需要添加请求标头。我尝试使用以下代码来执行此操作:

  /***********        Init request headers    ********/
  NameValueCollection requestHeaders = Request.Headers;
  foreach (string key in requestHeaders)
  {
      client.Headers.Add(key, requestHeaders[key]);
  }
  /***************************************************/

但是它不起作用,并且出现以下异常:

必须使用适当的属性修改此标头。参数名称:名称

有人可以帮助我吗?使用 WebClient 添加请求标头的正确方法是什么?

谢谢。

I have the following code with which I download a web-page into a byte array and then print it with Response.Write:

WebClient client = new WebClient();

byte[] data = client.DownloadData(requestUri);

  /***********        Init response headers    ********/
  WebHeaderCollection responseHeaders = client.ResponseHeaders;
  for (int i = 0; i < responseHeaders.Count; i++)
       {
            Response.Headers.Add(responseHeaders.GetKey(i), responseHeaders[i]);
       }
  /***************************************************/

Besides of the response headers, I need to add request headers as well. I try to do it with the following code:

  /***********        Init request headers    ********/
  NameValueCollection requestHeaders = Request.Headers;
  foreach (string key in requestHeaders)
  {
      client.Headers.Add(key, requestHeaders[key]);
  }
  /***************************************************/

However it does not work and I get the following exception:

This header must be modified using the appropriate property.Parameter name: name

Could anybody help me with this? What's the correct way of adding request headers with WebClient?

Thank you.

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

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

发布评论

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

评论(2

红尘作伴 2024-12-14 01:58:57

标头集合“保护”一些可能的标头,如 msdn 页面上所述:http://msdn.microsoft.com/en-us/library/system.net.webclient.headers.aspx

该页面似乎给出了您需要的所有答案,但引用了重要的内容部分:

一些常见的标头被认为是受限制的,并受到
系统,无法在 WebHeaderCollection 对象中设置或更改。
任何尝试在
与 WebClient 对象关联的 WebHeaderCollection 对象将
稍后尝试发送 WebClient 时抛出异常
请求。

受系统保护的受限标头包括但不包括
仅限于以下内容:

<前><代码>日期

主持人

此外,其他一些标头在使用时也会受到限制
WebClient 对象。这些受限标头包括但不包括
仅限于以下内容:

<前><代码>接受

联系

内容长度

Expect(当值设置为“100-继续”时

如果-修改-自

范围

传输编码

HttpWebRequest 类具有用于设置上述某些内容的属性
标头。如果应用程序设置这些标头很重要,
那么应该使用 HttpWebRequest 类而不是 WebRequest
类。

我怀疑其原因是许多标头(例如日期和主机)必须根据不同的请求进行不同的设置。你不应该复制它们。事实上,我个人可能建议您不要复制其中任何一个。放入您自己的用户代理 - 如果您获得的页面依赖于某个值,那么我认为您希望确保始终发送有效值,而不是依赖原始用户向您提供该信息。

从本质上讲,弄清楚你需要做什么,而不是找到一些可行的事情,然后在没有完全理解你在做什么的情况下就去做。

The headers collection "protects" some of the possible headers as described on the msdn page here: http://msdn.microsoft.com/en-us/library/system.net.webclient.headers.aspx

That page seems to give all the answer you need but to quote the important part:

Some common headers are considered restricted and are protected by the
system and cannot be set or changed in a WebHeaderCollection object.
Any attempt to set one of these restricted headers in the
WebHeaderCollection object associated with a WebClient object will
throw an exception later when attempting to send the WebClient
request.

Restricted headers protected by the system include, but are not
limited to the following:

Date

Host

In addition, some other headers are also restricted when using a
WebClient object. These restricted headers include, but are not
limited to the following:

Accept

Connection

Content-Length

Expect (when the value is set to "100-continue"

If-Modified-Since

Range

Transfer-Encoding

The HttpWebRequest class has properties for setting some of the above
headers. If it is important for an application to set these headers,
then the HttpWebRequest class should be used instead of the WebRequest
class.

I suspect the reason for this is that many of the headers such as Date and host must be set differently on a different request. You should not be copying them. Indeed I would personally probably suggest that you should not be copying any of them. Put in your own user agent - If the page you are getting relies on a certain value then I'd think you want to make sure you always send a valid value rather than relying on the original user to give you that information.

Essentially work out what you need to do rather than finding something that works and doing that without fully understanding what you are doing.

遗失的美好 2024-12-14 01:58:57

看起来您正在尝试设置一些标头,该标头必须使用 WebClient 属性之一(CachePolicyContentLengthContentType)进行设置

此外,盲目复制所有标头并不是很好,您需要只获取您真正需要的标头。

Looks like you're trying to set some header which is must be set using one of the WebClient properties (CachePolicy, ContentLength or ContentType)

Moreover, it's not very good to blindly copy all the headers, you need to get just those you really need.

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