WebClient - Connection: Keep-Alive ALWAYS present.
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.
Is this happening with .Net Core 3.0? Can you provide some sample code that shows it?
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?
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
WebClientclass. 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.
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).