4

I'm writing a program in Visual Studio 2010 using C# .Net

The program is to save the file from a given url to local drive, with a custom timeout to save time.

Say the url is http://mywebsite.com/file1.pdf, and I want to save the file to the directory C:\downloadFiles\

Currently, I'm using WebClient.

WebClient.DownloadFile("http://mywebsite.com/file1.pdf", "C:\downloadFiles\file1.pdf");

I am able to save the file, but I ran into some problem.

Sometimes, the url just won't respond, so I have my program try to download 5 times before terminating. I then realize the default timeout for WebClient is too long for my need (like 2 min or something) Is there a simple way to set timeout shorter, say like 15 sec?

I also looked into HttpWebRequest, which I can easily set the timeout HttpWebRequest.Timeout = 15000;. However, with this method, I have no idea how I can download/save the file.

So my over all questions is: Which is more simple, setting timeout for WebClient, or saving file using HttpWebRequest? And how would I go about doing so?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
sora0419
  • 2,308
  • 9
  • 39
  • 58
  • possible duplicate of [Set timeout for webClient.DownloadFile()](http://stackoverflow.com/questions/601861/set-timeout-for-webclient-downloadfile) – keyboardP Jun 28 '13 at 18:04
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 28 '13 at 18:26

4 Answers4

12

You can create your own WebClient

public class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var req = base.GetWebRequest(address);
        req.Timeout = 15000;
        return req;
    }
}
I4V
  • 34,891
  • 6
  • 67
  • 79
4

You could use the HttpClient class if you are using .NET 4.5:

using(var httpClient = new HttpClient())
{
    httpClient.Timeout = new TimeSpan(0, 0, 15);

    Stream response = await httpClient.GetStreamAsync("http://mywebsite.com/file1.pdf");

    ...
}

Here's an example that get's a json response in LinqPad: http://share.linqpad.net/aaeeum.linq

Samuel Parkinson
  • 2,992
  • 1
  • 27
  • 38
1

There's no direct way to change timeout with a WebClient. But you can use WebClient.DownloadFileAsync() instead. This will allow you to use CancelAsync() when needed.

RAS
  • 3,375
  • 15
  • 24
0

In .net 4 and above you can do something like

var wreq = WebRequest.Create("http://mywebsite.com/file1.pdf");
wreq.Timeout = 15000;

var wresp = (HttpWebResponse)request.GetResponse();

using (Stream file = File.OpenWrite("path/to/output/file.pdf"))
{
    wresp.GetResponseStream().CopyTo(file); // CopyTo extension only .net 4.0+ 
}

Otherwise you could copy it yourself

using (Stream file = File.OpenWrite("path/to/output/file.pdf"))
{
    var input = wresp.GetResponseStream();

    var buffer = new byte[8 * 1024];

    int len;
    while ((len = input.Read(buffer, 0, buffer.Length)) > 0) 
    {
        file.Write(buffer, 0, len);
    }
}
T I
  • 9,785
  • 4
  • 29
  • 51