Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout for Web Request

Tags:

c#

What is a reasonable amount of time to wait for a web request to return? I know this is maybe a little loaded as a question, but all I am trying to do is verify if a web page is available.

Maybe there is a better way?

try 
{ 
    // Create the web request 
    HttpWebRequest request = WebRequest.Create(this.getUri()) as HttpWebRequest;

    request.Credentials = System.Net.CredentialCache.DefaultCredentials;

    // 2 minutes for timeout
    request.Timeout = 120 * 1000;

    if (request != null)
    {
        // Get response 
        response = request.GetResponse() as HttpWebResponse;

        connectedToUrl = processResponseCode(response);
    }
    else
    {
        logger.Fatal(getFatalMessage());

        string error = string.Empty;
    }
} 
catch (WebException we) 
{ 
...
} 
catch (Exception e) 
{ 
...
} 
like image 988
steve_mtl Avatar asked Jan 20 '09 19:01

steve_mtl


2 Answers

You need to consider how long the consumer of the web service is going to take e.g. if you are connecting to a DB web server and you run a lengthy query, you need to make the web service timeout longer then the time the query will take. Otherwise, the web service will (erroneously) time out.

I also use something like (consumer time) + 10 seconds.

like image 57
rbrayb Avatar answered Sep 20 '22 16:09

rbrayb


Offhand I'd allow 10 seconds, but it really depends on what kind of network connection the code will be running with. Try running some test pings over a period of a few days/weeks to see what the typical response time is.

like image 23
David Z Avatar answered Sep 21 '22 16:09

David Z