简体   繁体   English

webclient 和 expect100continue

[英]webclient and expect100continue

What is the best way to set expect100continue when using WebClient(C#.NET).使用 WebClient(C#.NET) 时设置 expect100continue 的最佳方法是什么。 I have this code below, I still see 100 continue in the header. Stupid apache still complains with 505 error.我在下面有这段代码,我仍然在 header 中看到 100 继续。愚蠢的 apache 仍然抱怨 505 错误。

        string url = "http://aaaa.com";
        ServicePointManager.Expect100Continue = false;

        WebClient service = new WebClient();           
        service.Credentials = new NetworkCredential("username", "password");
        service.Headers.Add("Content-Type","text/xml");

        service.UploadStringCompleted += (sender, e) => CompleteCallback(BuildResponse(e));
        service.UploadStringAsync(new Uri(url), "POST", query);

Note: If I put the above in a console app and let it run - then I do not see the headers in fiddler.注意:如果我将上面的内容放在控制台应用程序中并让它运行 - 那么我在 fiddler 中看不到标头。 But, my code is embedded in a user library which is loaded by a WPF app.但是,我的代码嵌入在由 WPF 应用程序加载的用户库中。 So, Is there more to Expect100Continue in terms of thread, initialization, etc. Now, I think it is more of my code issue.那么,在线程、初始化等方面,Expect100Continue 是否还有更多内容。现在,我认为更多的是我的代码问题。

You need to set the Expect100Continue property on the ServicePoint used for the URI you're accessing: 您需要在用于访问的URI的ServicePoint上设置Expect100Continue属性:

var uri = new Uri("http://foo.bar.baz");
var servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.Expect100Continue = false;

The only way to do this is to create an override. 执行此操作的唯一方法是创建覆盖。

   public class ExpectContinueAware : System.Net.WebClient
    {
        protected override System.Net.WebRequest GetWebRequest(Uri address)
        {
            System.Net.WebRequest request = base.GetWebRequest(address);
            if (request is System.Net.HttpWebRequest)
            {
                var hwr = request as System.Net.HttpWebRequest;
                hwr.ServicePoint.Expect100Continue = false;
            }
            return request;
        }
    }

This works perfect. 这很完美。

Try to create the WebClient instanse after you change Expect100Continue to false. 将Expect100Continue更改为false后,尝试创建WebClient实例。 Or use a Webrequest instead of a WebClient 或者使用Webrequest而不是WebClient

One of my below approach also worked, 我的下面一种方法也奏效了,

In my constructor of class (service calling), you can set System.Net.ServicePointManager.Expect100Continue = false; 在我的类(服务调用)的构造函数中,您可以设置System.Net.ServicePointManager.Expect100Continue = false;

and then I have created BasicHttpBinding object and continued, it executed successfully. 然后我创建了BasicHttpBinding对象并继续,它执行成功。

You can modify your Web/App.Config, by adding:您可以通过添加以下内容来修改您的 Web/App.Config:

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

At least for me, if the faulty server gets patched, I can easily remove the setting without having to recompile.至少对我来说,如果故障服务器得到修补,我可以轻松删除设置而无需重新编译。

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

相关问题 即使将Expect100Continue设置为false,Webclient UploadData也会返回错误417 - Webclient UploadData returns error 417 even after putting expect100Continue to false C#Expect100Continue标头请求 - C# Expect100Continue header request 如何在编码的Visual Studio 2013 WebTestRequest上将Expect100Continue设置为false? - How does one set Expect100Continue to false on a Coded Visual Studio 2013 WebTestRequest? 正确处理期望失败(417),并在WCF客户端应用程序中更改Expect100Continue - Correcty handle Expectation failed (417) and change Expect100Continue in WCF client app 当 Expect100Continue header 被禁用时,HttpWebRequest 会引入显着延迟 - HttpWebRequest introduces significant latency when the Expect100Continue header is disabled 期望:100-继续 - Expect: 100-continue 如何在WinRT的HttpWebRequest中禁用“Expect:100 continue”标头 - How to disable the “Expect: 100 continue” header in WinRT's HttpWebRequest "如何为单个请求禁用 HttpWebRequest 中的“期望:100 继续”标头?" - How to disable the "Expect: 100 continue" header in HttpWebRequest for a single request? Windows Store应用程序的ServicePoint.Expect100Continue - ServicePoint.Expect100Continue for Windows Store Apps 如何在Windows应用商店应用中使用WCF时禁用Expect:100-Continue - How to disable Expect:100-Continue when using WCF in Windows Store App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM