tags:

views:

1017

answers:

3

I'm using the C# using the WebClient().

I was testing out what headers are sent, and I noticed that the following header is automatically added.

Connection : Keep-Alive

Is there any way to remove this?

A: 
client.Headers.Remove("Connection");

should do the trick

X-Cubed
Nope, it doesn't do the trick.
Unknown
+2  A: 

Use HttpWebRequest instead of WebClient (it's slightly less convenient, but not by very much) and set the KeepAlive property to false.

I haven't tested this - it's possible that it'll just change the value of the Connection header instead of removing it - but it's worth a try. The docs for the Connection property at least suggest that it only adds Keep-Alive.

Jon Skeet
One thing to note is that doing this doesn't *remove* the Connection header, although it does change it from "Connection: Keep-Alive" to "Connection: Close".
Scott Mitchell
+1  A: 

I had ran into the same issue this morning. Following on Skeet's hint, it can be achieved by passing HttpWebRequest to WebClient by inheriting it:

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).KeepAlive = false;
        }
        return request;
    }
}

Now sent headers will include Connection : close

Redha