runtime icon indicating copy to clipboard operation
runtime copied to clipboard

WebClient - Connection: Keep-Alive ALWAYS present.

Open kulashaker30 opened this issue 7 years ago • 4 comments

Issue Title

WebClient is always sending 'Connection: Keep-Alive'

General

I am having trouble with WebClient always getting timeout exception because the Connection header is always present in the request.

kulashaker30 avatar Sep 23 '19 14:09 kulashaker30

Is this happening with .Net Core 3.0? Can you provide some sample code that shows it?

scalablecory avatar Sep 23 '19 17:09 scalablecory

WebClient is one of the older APIs in .NET Framework and .NET Core. I suggest you consider using HttpClient.

The behavior you see is unfortunately the same on both .NET Framework and .NET Core. The use of the HTTP request header 'Connection:' cannot easily be controlled with the WebClient class. It will default to 'Connection: Keep-Alive'.

Even the code below is unable to change this.

var client = new WebClient();
//client.Headers.Clear(); // clears all request headers but 'Connection' and 'Host' are still present
//client.Headers[HttpRequestHeader.Connection] = "close"; // throws an exception
string result = client.DownloadString(server);

I am having trouble with WebClient always getting timeout exception because the Connection header is always present in the request.

That said, the use of 'Connection: Keep-Alive' is a default header and semantic for any HTTP/1.1 connection. So, the key question really is why do you think it is causing a timeout exception in your application?

davidsh avatar Sep 23 '19 20:09 davidsh

WebClient is one of the older APIs in .NET Framework and .NET Core. I suggest you consider using HttpClient.

The behavior you see is unfortunately the same on both .NET Framework and .NET Core. The use of the HTTP request header 'Connection:' cannot easily be controlled with the WebClient class. It will default to 'Connection: Keep-Alive'.

Even the code below is unable to change this.

var client = new WebClient();
//client.Headers.Clear(); // clears all request headers but 'Connection' and 'Host' are still present
//client.Headers[HttpRequestHeader.Connection] = "close"; // throws an exception
string result = client.DownloadString(server);

I am having trouble with WebClient always getting timeout exception because the Connection header is always present in the request.

That said, the use of 'Connection: Keep-Alive' is a default header and semantic for any HTTP/1.1 connection. So, the key question really is why do you think it is causing a timeout exception in your application?

I am getting this when sending a get request from an api service which returns gzip compression response. All the headers I added (e.g accept-encoding and Connection: keep-alive) are working in Fiddler BUT if I try to use 'Keep-Alive' I encounter the same issue I have in WebClient class.

I am using now HttpWebRequest because I can remove the Connection header from it unlike in WebClient which I am getting an error when I try to remove it.

kulashaker30 avatar Sep 25 '19 04:09 kulashaker30

I am using now HttpWebRequest because I can remove the Connection header from it unlike in WebClient which I am getting an error when I try to remove it.

It's good you found a workaround by using 'HttpWebRequest' instead of 'WebClient'.

We don't plan to fix 'WebClient' for this scenario since it is a legacy class and we expect customers to move to either 'HttpWebRequest' or 'HttpClient' (most recommended).

davidsh avatar Feb 24 '20 19:02 davidsh