Expect100Continue Issue

The HttpWebRequest class has by default a nice feature – it adds “Except: 100-continue” to request headers. Like this:

POST http://www.example.com/do HTTP/1.1
Host: www.example.com
Content-Length: 12
Expect: 100-continue
Connection: Keep-Alive

Ok, but some servers can respond with the error. Like this one did

HTTP/1.0 417 Expectation failed
Server: squid/2.7.STABLE9

It is not so easy to get rid of this header. Because it is set by an underlying logic when one grabs a response from the server. At first I tried System.Net.ServicePointManager.Expect100Continue static boolean property. But such approach smells a little bit crappy. And it did not work furthermore (I set this property before a request instance creating). But fortunately there is such property on per request basis. Check this code snippet and its bold line:

            var request = (HttpWebRequest)WebRequest.Create(_messagePostUri);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            using (var stream = request.GetRequestStream())
                FormMessageBody(stream, smsMessage, captcha);

            request.ServicePoint.Expect100Continue = false;

            using (var response = request.GetResponse())
                //TODO: check that we have all done

And great, it works pretty well

This entry was posted in .NET, C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s