使用 WebClient C# 添加请求标头 [英] Add request headers with WebClient C#

查看:165
本文介绍了使用 WebClient C# 添加请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

有人能帮我解决这个问题吗?使用 WebClient 添加请求标头的正确方法是什么?

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

谢谢.

推荐答案

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

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:

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

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

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

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

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

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.

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

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.

这篇关于使用 WebClient C# 添加请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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