Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 246
Roman Starkov Avatar asked Dec 28 '12 01:12

Roman Starkov


People also ask

What is expect 100-continue?

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.

What is expect100continue HttpWebRequest?

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.


2 Answers

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; 
like image 177
Roman Starkov Avatar answered Oct 11 '22 15:10

Roman Starkov


If you also need to set a proxy, make sure to do that first. Otherwise Expect100Continue will be reverted to true again. So:

HttpWebRequest webRequest = WebRequest.CreateHttp(_url); webRequest.Proxy = new WebProxy(_proxyHost, _proxyPort); webRequest.ServicePoint.Expect100Continue = false; 
like image 41
AroglDarthu Avatar answered Oct 11 '22 13:10

AroglDarthu