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

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

问题描述

当我尝试在 WebRequest 对象上添加 HTTP 标头键/值对时,出现以下异常:

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() 方法向 Headers 集合添加新值,但仍然遇到相同的异常.

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 ="http://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.

感谢 FOR 和 Jvenema 提供的主要指导方针...

Thanks FOR and Jvenema for the leading guidelines...

  1. 但是,我发现,那是拼图中缺少的部分:

2.1 WebHeaderCollection类一般通过WebRequest.Headers或WebResponse.Headers访问.一些常见的标头被认为是受限制的,要么由 API 直接公开(例如 Content-Type),要么受系统保护且无法更改.

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.

受限制的标题是:

  • 接受
  • 连接
  • 内容长度
  • 内容类型
  • 日期
  • 期待
  • 主机
  • If-Modified-Since
  • 范围
  • 推荐人
  • 传输编码
  • 用户代理
  • 代理连接

所以,下次你遇到这个异常并且不知道如何解决这个问题时,请记住有一些受限制的标头,解决方案是使用 WebRequest<中的适当属性显式修改它们的值/code>/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.

(有用,来自评论,用户评论 凯多)

(useful, from comments, comment by user Kaido)

解决方案是在调用 add 之前检查 header 是否已经存在或被限制(WebHeaderCollection.IsRestricted(key))

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天全站免登陆