Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webclient and expect100continue

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 505 error.

        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. But, my code is embedded in a user library which is loaded by a WPF app. So, Is there more to Expect100Continue in terms of thread, initialization, etc. Now, I think it is more of my code issue.

like image 883
neblinc1 Avatar asked May 06 '10 21:05

neblinc1


People also ask

What is expect100continue?

The client will expect to receive a 100-Continue response from the server to indicate that the client should send the data to be posted. This mechanism allows clients to avoid sending large amounts of data over the network when the server, based on the request headers, intends to reject the request.


2 Answers

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;
like image 84
Thomas Levesque Avatar answered Oct 15 '22 23:10

Thomas Levesque


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.

like image 30
Syv Development Avatar answered Oct 16 '22 01:10

Syv Development