Select Page

With the .NET framework it has become very easy to programmatically download content from the internet. Using the WebClient class from the System.Net downloading a file from the net becomes as easy as specifying the address to download.
There are several different ways to accomplish this depending on the type of file you want to get, and also the content of the file.

Downloading a string

The following code uses a WebClient to download the file returned by http://bitmatic.com. The file is returned as a string, so it can be directly assigned to, for instance, a RichTextBox. With basically one line of code you have built yourself a (very) primitive web-browser. The method is very good if you just want a simple textual presentation of a web-content, or you need to work with the content in a textual manner with regular expressions or something similar.

using (WebClient client = new WebClient())
{
  richTextBox.Text = client.DownloadString("http://bitmatic.com");
}

The WebClient object is scoped with the “using”-keyword. This is because it uses ressources that needs to be disposed properly. You could of course just ignore this and let the garbage collector dispose of it at some indefinite time, but that way you will run the risk of running out of ressources before the garbage collector cleans up (and don’t even think about calling the garbage collector manually… just don’t). You could also call client.Dispose(), but i mostly prefer the “using” syntax.

Downloading a and saving a file

Downloading and saving a file to your local filesystem is equally easy. Just supply an address and a filename to the DownloadFile method, and you have downloaded a file. You can download both using http and ftp with this method. The framework will handle this for you in a transparent way.

using (WebClient client = new WebClient())
{
  client.DownloadFile("http://bitmatic.com", "bitmatic.html");
}

This code saves the content at http://bitmatic.com in a file called “bitmatic.html” on the local file system.

Downloading the raw data in a byte array

Lastly i want to mention a method that returns the content as a byte array. This will mostly be usefull for binary files, since the text encoding may be lost when using this method. You could look into the ResponseHeaders property of the WebClient after getting the data, they may contain information about encoding if the content was a text file, but really, you should just use DownloadString for text files.

using (WebClient client = new WebClient())
{
  byte[] bArr = client.DownloadData("http://bitmatic.com");
}

Only half the story

The WebClient is a very powerfull class, and the tiny code examples above should only serve as an appetizer. The class also allows you to do all the operations asynchronously, and perform uploads as well as downloads. If you start to look into modifying the headers that are sent you could get really creative. Take a look at the msdn article about WebClient for further inspiration.