Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set timeout for webClient.DownloadFile()

I'm using webClient.DownloadFile() to download a file can I set a timeout for this so that it won't take so long if it can't access the file?

like image 999
UnkwnTech Avatar asked Mar 02 '09 10:03

UnkwnTech


People also ask

What is the default timeout for WebClient?

java example code the WebClient build using the default builder without any specific configuration. Hence it falls back to the default connect and read timeout, which is 30 seconds each. Modern applications do not wait for 30 seconds for anything.

What is WebClient DownloadData?

The DownloadData method downloads the resource with the URI specified by the address parameter. This method blocks while downloading the resource. To download a resource and continue executing while waiting for the server's response, use one of the DownloadDataAsync methods.

How to download file using WebClient in c#?

Download Files from Web [C#] The simply way how to download file is to use WebClient class and its method DownloadFile. This method has two parameters, first is the url of the file you want to download and the second parameter is path to local disk to which you want to save the file.

What is net WebClient?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.


1 Answers

My answer comes from here

You can make a derived class, which will set the timeout property of the base WebRequest class:

using System; using System.Net;  public class WebDownload : WebClient {     /// <summary>     /// Time in milliseconds     /// </summary>     public int Timeout { get; set; }      public WebDownload() : this(60000) { }      public WebDownload(int timeout)     {         this.Timeout = timeout;     }      protected override WebRequest GetWebRequest(Uri address)     {         var request = base.GetWebRequest(address);         if (request != null)         {             request.Timeout = this.Timeout;         }         return request;     } } 

and you can use it just like the base WebClient class.

like image 158
Beniamin Avatar answered Oct 12 '22 23:10

Beniamin