webclient and expect100continue

后端 未结 4 1032
傲寒
傲寒 2021-02-14 21:52

What is the best way to set expect100continue when using WebClient(C#.NET). I have this code below, I still see 100 continue in the header. Stupid apache still complains with 50

相关标签:
4条回答
  • 2021-02-14 22:34

    You need to set the Expect100Continue property on the ServicePoint used for the URI you're accessing:

    var uri = new Uri("http://foo.bar.baz");
    var servicePoint = ServicePointManager.FindServicePoint(uri);
    servicePoint.Expect100Continue = false;
    
    0 讨论(0)
  • 2021-02-14 22:34

    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.

    0 讨论(0)
  • 2021-02-14 22:34

    Try to create the WebClient instanse after you change Expect100Continue to false. Or use a Webrequest instead of a WebClient

    0 讨论(0)
  • 2021-02-14 22:44

    One of my below approach also worked,

    In my constructor of class (service calling), you can set System.Net.ServicePointManager.Expect100Continue = false;

    and then I have created BasicHttpBinding object and continued, it executed successfully.

    0 讨论(0)
提交回复
热议问题