C#:Tutorial - Download Data

Partial codes that may help you further your coding education.
Post Reply
User avatar
The Big Kahuna
Posts: 76
Joined: Thu Jul 31, 2008 12:02 am
Location: Portland, OR

C#:Tutorial - Download Data

Post by yang » Sun Aug 03, 2008 3:05 am

How to Download Data in C# .NET

In this tutorial you will learn how to download data the internet and use it in your applications. I am using C#, but you can use any .NET language, with minor alterations to the code.

The .NET Framework (currently version 3.5) provides many useful classes for us to use in our applications. For the internet, we use the System.Net namespace. There are many different objects to instantiate, and each one has a specific use.

The extremely easy way to download data is to use WebClient.DownloadData(). However, for more control, we will use multiple objects. Here's what we do:

Full Code

Here's the code in its entirety. You need to add the following statement to the very top of the code file. At the top, find the group of statements that says "using System;" and so on, and add this one to the bottom:

Code: Select all

using System.Net;
Now, in the location you want the code to happen, type this code:

Code: Select all

//Replace the following address with the address of the file you want
//to download from. Here, the program will download the XML file that
//contains the RSS Feed for the forums:

string strUrl = "http://forum.codecall.net/external.php?type=RSS2";

WebRequest request = WebRequest.Create(strUrl);
WebResponse response = request.GetResponse();
string data = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();

//Now, the XML File is stored in the local variable "data".
And that's all there is to it! To put the data inside a textbox called "txtData", then use this:

Code: Select all

//Put the data into the text box.
txtData.Text = data;
Code Explanation

There are quite a few objects involved, so let's take a minute to dissect the code.

First of all, we create a new WebRequest object. However, we cannot do this just by using the 'new' keyword. Instead, we use WebRequest.Create(). The Create() method expects one parameter - the URL of the file to download.

There is a method of the new WebRequest object called GetResponse(). This, unsurprisingly, returns a WebResponse object. In other words, we ask the computer to request the data, and the server containing the data responds back (if we've done it right).

Next, the new WebResponse object has the method we need to call in order to actually receive the data - GetResponseStream(). This returns a System.IO.Stream, so we can read it using a StreamReader object under the same namespace.


And that's all there is to it! Once you know the objects, you can utilise them to download the data. Think about it - you could use this program to check whether updates are available, get the latest up-to-date data... the possibilities are endless.
"To know, is to know that you know nothing. That is the meaning of true knowledge." - Confucius
"知之为知之,不知为不知,是知也。" - 孔子

Post Reply