10

I see the post from https://stackoverflow.com/questions/6262547/webclient-timeout-error , it says the default timeout is 100 seconds. But I see the comment from How to change the timeout on a .NET WebClient object says

The default timeout is 100 seconds. Although it seems to run for 30 seconds. – Carter Dec 13 '12 at 16:39

In my program the timeout always about 20 seconds, does anybody know the reason?

Community
  • 1
  • 1
MichaelMao
  • 2,596
  • 2
  • 23
  • 54
  • HttpWebRequest Timeout is 100 seconds, Session timeout is 20 minutes. – sansknwoledge Dec 04 '15 at 07:50
  • 2
    Sorry @sansknwoledge, this not help. – MichaelMao Dec 04 '15 at 07:52
  • 1
    yep , that was pumped up comment, have you checked out msdn https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx , especially the last para, – sansknwoledge Dec 04 '15 at 07:57
  • Yes, I have checked that, but I think it's the different issue. My question is if default timeout is 100 seconds. Why my program will timeout during 20 seconds and the comment say 30 seconds. – MichaelMao Dec 04 '15 at 08:04
  • 1
    With HttpClient I'm seeing similar 20 second timeout. Changing the TimeOut setting does not raise it any higher (but does lower it). .NET Core project issue tracker has some discussion about an issue which might be related: https://github.com/dotnet/corefx/issues/2857 – Juha Palomäki Jan 17 '17 at 21:37
  • 20 seconds is very close to Windows' default connection timeout of 21 seconds. So I'd assume your requests failed because the server wasn't reachable, wasn't running or just didn't accept the connection. In which case all `WebClient`/`HttpWebRequest` timeouts are irrelevant. See my answer for some more details. – Paul Groke Oct 01 '22 at 16:11

2 Answers2

13

I put together a minimal case to test the WebClient class's default timeout.

I published a simple website to my local PC which, upon receiving a request, waits 300 seconds (long enough to make WebClient time out), and then returns a response.

I wrote a simple program which uses a WebClient to make a request to that site, and report what happens:

void Main()
{
    Console.WriteLine("Starting request at " + DateTime.Now);
    WebClient client = new WebClient();
    try
    {
        string response = client.DownloadString("http://slowsite.local/");
        Console.WriteLine("Response returned at " + DateTime.Now);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.GetType() + " " + ex.Message + " at " + DateTime.Now);
    }
}

The WebClient did time out after 100 seconds. The program produced this output:

Starting request at 8/1/2017 9:31:11 AM
System.Net.WebException The request was aborted: The operation has timed out. at 8/1/2017 9:32:51 AM

The client test program targeted .NET Framework 4.6.

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
  • Hi Jon, good test but someone got same problem with me so I can't choose you answer as the real answer – MichaelMao Aug 02 '17 at 04:42
  • 3
    Can you post minimal repro code, so we can see what your program is doing differently than my code? It might also be helpful if you could post the details of the exception you are getting, and the .NET version your program uses. – Jon Schneider Aug 02 '17 at 04:54
  • Give me some days LOL – MichaelMao Aug 02 '17 at 05:11
3

There are multiple timeouts at play here.

If the connection has not yet been established and the host is not specified with an IP literal, then the first thing that happens is name resolution. If the DNS requests all time out (default should be after ~15 seconds on Windows), then the request will fail, regardless of what timeouts are configured on the HttpWebRequest.

Then the transport level connection has to be established. In case of TCP, Windows' default timeout for that is 21 seconds. Same here: if this fails, the request fails, regardless of what's configured on the HttpWebRequest.

Next comes the part where the HttpWebRequest waits for the response headers. This is controlled by the Timeout property, which defaults to 100 seconds.

And then comes reading the response data. According to the documentation, this is controlled by ReadWriteTimeout, which defaults to 5 minutes. Note however that this is not the timeout for receiving all data. As long as some new data is being received with no "silent" phase of more than ReadWriteTimeout, the request will not time out.

Paul Groke
  • 6,259
  • 2
  • 31
  • 32