Friday, November 18, 2011

HTTP/1.1 WebRequest with the Expect: 100-continue header

Ran into an issue today when calling a web service from .NET.
If the Expect: 100-continue HTTP header was included in the request the web server would flip out and return a response that couldn't be parsed. Fiddler gave it as:

HTTP Protocol Violation
Fiddler has detected a protocol violation in session #41.

Cannot parse HTTP response; Status line contains no spaces. Data:

 0
[Fiddler] Response Header parsing failed.
Response Data:
<plaintext>
30 0D 0A 0D 0A                                                           0....                

In .NET is appeared as:

The server committed a protocol violation. Section=ResponseStatusLine

Apparently this header is added automatically by .NET when doing a POST with HTTP/1.1 to indicate that the post data isn't sent with the initial request. So if the web server rejects the request we hasn't wasted time sending all the data up. This has also been a known issue for some time now - See HttpWebRequest and the Expect: 100-continue Header Problem from 2004.

The solution to stop the header being added is relatively simple:

WebRequest request = null;
//...
((HttpWebRequest)request).ServicePoint.Expect100Continue = false;