WebClient default timeout?
Asked Answered
C

2

10

I see the post from https://mcmap.net/q/11546/-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?

Chambers answered 4/12, 2015 at 7:45 Comment(6)
HttpWebRequest Timeout is 100 seconds, Session timeout is 20 minutes.Buggs
Sorry @sansknwoledge, this not help.Chambers
yep , that was pumped up comment, have you checked out msdn msdn.microsoft.com/en-us/library/… , especially the last para,Buggs
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.Chambers
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: github.com/dotnet/corefx/issues/2857Minnich
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.Linderman
O
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.

Oxtail answered 1/8, 2017 at 13:42 Comment(3)
Hi Jon, good test but someone got same problem with me so I can't choose you answer as the real answerChambers
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.Oxtail
Give me some days LOLChambers
L
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.

Linderman answered 1/10, 2022 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.