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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With