<bdo id='iK8il'></bdo><ul id='iK8il'></ul>

<small id='iK8il'></small><noframes id='iK8il'>

  • <legend id='iK8il'><style id='iK8il'><dir id='iK8il'><q id='iK8il'></q></dir></style></legend>

    1. <i id='iK8il'><tr id='iK8il'><dt id='iK8il'><q id='iK8il'><span id='iK8il'><b id='iK8il'><form id='iK8il'><ins id='iK8il'></ins><ul id='iK8il'></ul><sub id='iK8il'></sub></form><legend id='iK8il'></legend><bdo id='iK8il'><pre id='iK8il'><center id='iK8il'></center></pre></bdo></b><th id='iK8il'></th></span></q></dt></tr></i><div id='iK8il'><tfoot id='iK8il'></tfoot><dl id='iK8il'><fieldset id='iK8il'></fieldset></dl></div>

        <tfoot id='iK8il'></tfoot>
      1. 使用 System.Net.WebRequest 时无法设置某些 HTTP 标头

        时间:2023-08-26
        <legend id='2w0xn'><style id='2w0xn'><dir id='2w0xn'><q id='2w0xn'></q></dir></style></legend>

              <tfoot id='2w0xn'></tfoot>
            1. <i id='2w0xn'><tr id='2w0xn'><dt id='2w0xn'><q id='2w0xn'><span id='2w0xn'><b id='2w0xn'><form id='2w0xn'><ins id='2w0xn'></ins><ul id='2w0xn'></ul><sub id='2w0xn'></sub></form><legend id='2w0xn'></legend><bdo id='2w0xn'><pre id='2w0xn'><center id='2w0xn'></center></pre></bdo></b><th id='2w0xn'></th></span></q></dt></tr></i><div id='2w0xn'><tfoot id='2w0xn'></tfoot><dl id='2w0xn'><fieldset id='2w0xn'></fieldset></dl></div>

                <tbody id='2w0xn'></tbody>

                <small id='2w0xn'></small><noframes id='2w0xn'>

                  <bdo id='2w0xn'></bdo><ul id='2w0xn'></ul>
                • 本文介绍了使用 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 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    <bdo id='RgSCV'></bdo><ul id='RgSCV'></ul>
                  • <small id='RgSCV'></small><noframes id='RgSCV'>

                      <legend id='RgSCV'><style id='RgSCV'><dir id='RgSCV'><q id='RgSCV'></q></dir></style></legend>
                        <tbody id='RgSCV'></tbody>
                    1. <i id='RgSCV'><tr id='RgSCV'><dt id='RgSCV'><q id='RgSCV'><span id='RgSCV'><b id='RgSCV'><form id='RgSCV'><ins id='RgSCV'></ins><ul id='RgSCV'></ul><sub id='RgSCV'></sub></form><legend id='RgSCV'></legend><bdo id='RgSCV'><pre id='RgSCV'><center id='RgSCV'></center></pre></bdo></b><th id='RgSCV'></th></span></q></dt></tr></i><div id='RgSCV'><tfoot id='RgSCV'></tfoot><dl id='RgSCV'><fieldset id='RgSCV'></fieldset></dl></div>

                          <tfoot id='RgSCV'></tfoot>