If I run the following code, which tries to GET a HTTPS URL on an endpoint that is not answering (notice the port); then it throws a timeout WebException when a second has passed, as I would expect:
var request = WebRequest.CreateHttp("https://stackoverflow.com:81");
request.Timeout = 1000; // ms
Stopwatch watch = Stopwatch.StartNew();
try
{
using(var response = request.GetResponse())
{
new StreamReader(response.GetResponseStream()).ReadToEnd();
}
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
watch.Stop();
Console.WriteLine(watch.Elapsed);
Now, if I connect via a proxy (for instance, by opening Fiddler), the timeout is not honored, and the exception only occurs after 21 seconds has passed (!). I suspect the timeout is proxy-specific (the actual duration is larger when I try to connect via another proxy we have internally).
- This does not happen if the server is listening on the port and returns an error.
- This does not happen for HTTP, only HTTPS
- I tried it on .NET 4.5 and 4.5.1 with same results.
Why does this happen ? Is is a bug in HttpWebRequest ?
Is there any way to work around this (which preferably does not involve setting up my own timer thread for enforcing a timeout) ?