使用 WebClient C# 添加请求标头
我有以下代码,用于将网页下载到字节数组中,然后使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标头集合“保护”一些可能的标头,如 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:
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 属性之一(
CachePolicy
、ContentLength
或ContentType
)进行设置此外,盲目复制所有标头并不是很好,您需要只获取您真正需要的标头。
Looks like you're trying to set some header which is must be set using one of the WebClient properties (
CachePolicy
,ContentLength
orContentType
)Moreover, it's not very good to blindly copy all the headers, you need to get just those you really need.