aitee
aitee

Reputation: 91

WebClient Timeout solution doesn't work (subclassing to set Timeout)

I am trying to use this as a commonly accepted solution for setting a timeouts for WeblClient calls. In my test case of requesting a url from an offline machine: I consistently get 20 second timeouts when set at 1 second.

public class TimeoutWebClient : WebClient
{
    public TimeoutWebClient(TimeSpan timeout)
    {
        _timeout = timeout;
    }
    private TimeSpan _timeout;

   protected override WebRequest GetWebRequest(Uri address)
   {
       WebRequest request = base.GetWebRequest(address);
       request.Timeout = (int)_timeout.TotalMilliseconds;
       return request;
   }
}

Server based timeouts can't matter in this scenario. What could I be missing?

I did find a snippet that set both HttpWebRequest.ReadWriteTimeout and HttpWebRequest.ServicePoint.MaxIdleTime. But setting these to the timeout value still didn't make a difference and am still getting ~20 second timeouts.

Any other thoughts of what could cause this behavior?

Upvotes: 1

Views: 466

Answers (1)

atikot
atikot

Reputation: 5041

Any chance you are using async download method?

From the MSDN:

The Timeout property affects only synchronous requests made with the GetResponse method. To time out asynchronous requests, use the Abort method.

Upvotes: 1

Related Questions