简体   繁体   English

"如何为单个请求禁用 HttpWebRequest 中的“期望:100 继续”标头?"

[英]How to disable the "Expect: 100 continue" header in HttpWebRequest for a single request?

HttpWebRequest<\/code> automatically appends an Expect: 100-continue<\/code> header for POST requests. HttpWebRequest<\/code>自动为 POST 请求附加一个Expect: 100-continue<\/code>标头。 Various sources around the internet suggest that this can be disabled as follows:互联网上的各种消息来源表明,可以按如下方式禁用此功能:

System.Net.ServicePointManager.Expect100Continue = false;

The HttpWebRequest class has a property called ServicePoint which can be used to change this setting for a specific request. HttpWebRequest类有一个名为ServicePoint的属性,可用于更改特定请求的此设置。 For example:例如:

var req = (HttpWebRequest) WebRequest.Create(...);
req.ServicePoint.Expect100Continue = false;

If you also need to set a proxy, make sure to do that first.如果您还需要设置代理,请务必先执行此操作。 Otherwise Expect100Continue will be reverted to true again.否则Expect100Continue将再次恢复为true So:所以:

HttpWebRequest webRequest = WebRequest.CreateHttp(_url);
webRequest.Proxy = new WebProxy(_proxyHost, _proxyPort);
webRequest.ServicePoint.Expect100Continue = false;

This can also be overwritten in the config:这也可以在配置中覆盖:

<system.net>
    <settings>
        <servicePointManager expect100Continue="false"/>
    </settings>
</system.net>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM