tags:

views:

1033

answers:

8

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) 
{ 
...
}
+2  A: 

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.

David Zaslavsky
+1  A: 

The reasonable amount of time to wait for a web request may differ from one server to the next. If a server is at the far end of a high-delay link then clearly it will take longer to respond than when it is in the next room. But two minutes seems like it's more than ample time for a server to respond. The default timeout value for the PING command is expressed in seconds, not minutes. I suggest you look into the timeout values that are used by networking utilities like PING or TRACERT for inspiration.

Seventh Element
+1  A: 

I would only wait (MAX) 30 seconds probably closer to 15. It really depends on what you are doing and what the result is of unsuccessful connection. As I am sure you know there is lots of reason why you could get a timeout...

cgreeno
+1  A: 

I guess this depends on two things:

  • network speed/load (as others wrote, using ping might give you an idea about this)
  • the kind of page you are calling: e.g. is it a static HTML page or is it a page which might do some time-consuming operations (DB access, etc.)

Anyway, I think 2 minutes is a lot of time. I would definitely reduce the timeout to less than 30 seconds.

M4N
+4  A: 

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.

nzpcmad
+2  A: 

I would measure how long it takes for pages that do exist to respond. If they all respond in about the same amount of time, then I would set the timeout period to approximately double that amount.

Sean Reilly
+1  A: 

I realize this doesn't directly answer your question, but then an "answer" to this question is a little tough. Anyway, a tool I've used gomez in the past to measure page load times from various parts of the world. It's free and if you haven't done this kind of testing before it might be helpful in terms of giving you a firm idea of what typical page load times are for a given page from a given location.

codemonkey
+2  A: 

Just wanted to add that a lot of the time I'll use an adaptive timeout. Could be a simple metric like:

period += (numTimeouts/numRequests > .01 ? someConstant: 0);

checked whenever you hit a timeout to try and keep timeouts under 1% (for example). Just be careful about decrementing it too low :)

annakata