Monday, January 15, 2007

HTTP Request issues – "Expect: 100-continue" and "Connection: Keep-Alive" headers


I have been working on a little .NET application (1.1) tool to send other HTTP the content from an XML file.

As I was intercepting the HTTP requests/responses going on between my machine and the server, I found out that my .NET application was always sending as part of the headers:

  • "Expect: 100-continue"
  • "Connection: Keep-Alive"

My application will always send a HTTP request in one go, get the response and exit. So, I need to get rid of those two HTTP headers (or at least disable them).

Here a sample from “TCPTrace”:
# The HTTP request


POST /HTTPFlowerServer/Cancellation.aspx HTTP/1.1
Content-Type: text/xml
Content-Length: 89
Expect: 100-continue
Connection: Keep-Alive

Host: localhost:8080

<?xml version='1.0' encoding='utf-8'?>
<MyRequest><Cancellation Id="27" /></MyRequest>


# The HTTP response


HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Mon, 15 Jan 2007 18:23:03 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Set-Cookie: ASP.NET_SessionId=sq2f2p55jqjluojh3mr4qh3o; path=/
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 74

<MyResponse><Acknowledgement OrderId="27"></Acknowledgement></MyResponse>


In order to not keep the connection alive, you need change the “KeepAlive” property of your HttpWebRequest.

In order to remove the “Expect: 100-continue”, you need to change the settings on the ServicePoint used by your request.

Here is what you need to do:


HttpWebRequest myRequest = (HttpWebRequest)System.Net.WebRequest.CreateDefault(targetURI);
myRequest.Method = "POST";
myRequest.KeepAlive = false;
myRequest.ServicePoint.Expect100Continue = false;


I found the latter solution from a blog at http://haacked.com/archive/2004/05/15/449.aspx. Look for the entry made by Mirronelli.

Here a sample of the HTTP headers after the changes:

# The HTTP request


POST /HTTPFlowerServer/Cancellation.aspx HTTP/1.1
Content-Type: text/xml
Content-Length: 89
Connection: Close
Host: localhost:8080

<?xml version='1.0' encoding='utf-8'?> <MyRequest><Cancellation Id="27" /></MyRequest>

# The HTTP response


HTTP/1.1 200 OK
Connection: close
Date: Mon, 15 Jan 2007 19:00:53 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Set-Cookie: ASP.NET_SessionId=npxysrbhbz4rsi455x5qhe45; path=/
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 74

<MyResponse><Acknowledgement OrderId="27"></Acknowledgement></MyResponse>

No comments: