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

Roman Starkov picture Roman Starkov · Dec 28, 2012 · Viewed 38.8k times · Source

HttpWebRequest automatically appends an Expect: 100-continue header for POST requests. Various sources around the internet suggest that this can be disabled as follows:

System.Net.ServicePointManager.Expect100Continue = false;

However, I'm writing a library and I cannot disable this for the entire appdomain, in case the application relies on this behaviour. Nor can I assume that it will remain set to this value. How can I disable it for a specific request?

Answer

Roman Starkov picture Roman Starkov · Dec 28, 2012

The HttpWebRequest class has a property called ServicePoint which can be used to change this setting for a specific request. For example:

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