Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient default timeout?

Tags:

c#

asp.net

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?

like image 952
MichaelMao Avatar asked Dec 04 '15 07:12

MichaelMao


People also ask

What is default timeout of WebClient C#?

The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan. A Domain Name System (DNS) query may take up to 15 seconds to return or time out.

What is the default timeout for the RestTemplate?

The default timeout is infinite. By default RestTemplate uses SimpleClientHttpRequestFactory and that in turn uses HttpURLConnection.

What is timeout in RestTemplate?

RestTemplate default timeout Look inside the class source and you will find this. RestTemplate default timeout. private int connectTimeout = - 1 ; private int readTimeout = - 1 ; By default, resttemplate uses timeout property from JDK installed on the machine which is always infinite in not overridden.

What is the difference between WebClient and RestTemplate?

RestTemplate uses Java Servlet API and is therefore synchronous and blocking. Conversely, WebClient is asynchronous and will not block the executing thread while waiting for the response to come back. The notification will be produced only when the response is ready. RestTemplate will still be used.


1 Answers

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.

like image 69
Jon Schneider Avatar answered Oct 04 '22 20:10

Jon Schneider