I am writing a program in Visual Studio 2010 using C # .Net
The program is designed to save a file from a given URL on a local disk with a user timeout in order to save time.
Say the url is http://mywebsite.com/file1.pdf and I want to save the file in the directory C:\downloadFiles\
I am currently using WebClient .
WebClient.DownloadFile("http://mywebsite.com/file1.pdf", "C:\downloadFiles\file1.pdf");
I can save the file, but I had a problem.
Sometimes the URL just won't respond, so I have a program that you try to download 5 times before exiting. Then I understand that the default timeout for WebClient is too long for my need (e.g. 2 minutes or something else). Is there an easy way to set a timeout shorter, for example 15 seconds?
I also looked through the HttpWebRequest , which I can easily set the timeout to HttpWebRequest.Timeout = 15000; . However, with this method, I have no idea how I can upload / save a file.
So, on all issues:. Which is easier, set a timeout for WebClient or save the file using HttpWebRequest ? And how would I do that?
source share