How to Change the Timeout on a .Net Webclient Object

How to change the timeout on a .NET WebClient object

You can extend the timeout: inherit the original WebClient class and override the webrequest getter to set your own timeout, like in the following example.

MyWebClient was a private class in my case:

private class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest w = base.GetWebRequest(uri);
w.Timeout = 20 * 60 * 1000;
return w;
}
}

Set timeout for webClient.DownloadFile()

Try WebClient.DownloadFileAsync(). You can call CancelAsync() by timer with your own timeout.

How to Set TimeOut for WebClient on .net?

From the MSDN Doc:

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

So if you are going to do an asynchronous request, I think you need to manage a timer of your own, and call .Abort() on the instance after whatever period of time.

There is some sample code showing this on this MSDN page for the .Abort() method.



Related Topics



Leave a reply



Submit