C# Download a File from the Intenet
Use WebClient class to download an URL to string or file with timeout
Download using System.Net.WebClient class
The easiest way to download an URL to file or string in C# is using the System.Net.WebClient class.
Download URL to file using WebClient class:
using System.Net;
WebClient wc = new WebClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
wc.DownloadFile("http://www.example.com/somefile.txt",
@"c:\temp\somefile.txt");
Download URL to string using WebClient class:
using System.Net;
WebClient wc = new WebClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string
somestring = wc.DownloadString("http://www.example.com/somefile.txt");
The download function will throw a WebException if the download fails, so we need to put the function in a try {} block.
using System.Net;
string somestring;
try
{
WebClient
wc = new WebClient();
ServicePointManager.SecurityProtocol
= SecurityProtocolType.Tls12;
somestring = wc.DownloadString("http://www.example.com/somefile.txt");
}
catch (WebException we)
{
// add some kind of error processing
MessageBox.Show(we.ToString());
}
System.Net.WebClient with Timeout
The WebClient class has no timeout property, but we can override
GetWebRequest(Uri address) in our own derived class.
Please note, that the timeout property in GetWebRequest affects synchronous
requests only - it will not work for asynchronous requests.
using System.Net;
public class
WebClientWithTimeout:WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest wr = base.GetWebRequest(address);
wr.Timeout = 5000; // timeout in milliseconds (ms)
return wr;
}
}
...
string somestring;
try
{
WebClient wc = new
WebClientWithTimeout();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
somestring = wc.DownloadString("http://www.example.com/somefile.txt");
}
catch (WebException we)
{
// add some kind of error processing
MessageBox.Show(we.ToString());
}
Server Error 500 (Internal Server Error)
The web server may return 500 (Internal Server Error) if the user agent header
is missing.
Add "user-agent" to the http headers:
...
WebClient wc = new WebClient();
wc.Headers.Add
("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko)");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
somestring = wc.DownloadString("http://www.example.com/somefile.txt");
...