I am trying to download a client's data to my local machine (programatically) and their webserver is very, very slow which is causing a timeout in my WebClient
object.
Here is my code:
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
webClient.DownloadFile(downloadUrl, downloadFile);
Is there a way to set an infinite timeout on this object? Or if not can anyone help me with an example on an alternate way to do this?
The URL works fine in a browser - it just takes about 3 minutes to show.
As Sohnee says, using
System.Net.HttpWebRequest
and set theTimeout
property instead of usingSystem.Net.WebClient
.You can't however set an infinite timeout value (it's not supported and attempting to do so will throw an
ArgumentOutOfRangeException
).I'd recommend first performing a HEAD HTTP request and examining the
Content-Length
header value returned to determine the number of bytes in the file you're downloading and then setting the timeout value accordingly for subsequentGET
request or simply specifying a very long timeout value that you would never expect to exceed.Couldn't get the w.Timeout code to work when pulled out the network cable, it just wasn't timing out, moved to using HttpWebRequest and does the job now.
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
For completeness, here's kisp's solution ported to VB (can't add code to a comment)
The first solution did not work for me but here is some code that did work for me.