Lost Password?

  #1 (permalink)  
Old 04-18-2008, 03:01 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Cool C#:Tutorial - Download Data

How to Download Data in C# .NET

Hi, Xav here. 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:
using System.Net;
Now, in the location you want the code to happen, type this code:

Code:
//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:
//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.

Conclusion

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.

Look out for a Part II to the Download Data tutorial, coming soon!

Xav

P.S. Well, that's my first tutorial. How did I do?
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-20-2008, 06:58 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,345
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default Re: C#:Tutorial - Download Data

Very nice tutorial!
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-20-2008, 08:04 PM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 25
Posts: 4,512
Last Blog:
Zend: PHP Security Web...
Rep Power: 50
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: C#:Tutorial - Download Data

Great tutorial. 30 points awarded.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-21-2008, 12:31 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: C#:Tutorial - Download Data

Great! Do I get points every time a write a useful tutorial, then?
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-21-2008, 12:33 PM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 25
Posts: 4,512
Last Blog:
Zend: PHP Security Web...
Rep Power: 50
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: C#:Tutorial - Download Data

While the contest lasts, yes.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 04-21-2008, 01:04 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: C#:Tutorial - Download Data

Oh - how long does the contest last?
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-21-2008, 01:14 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,345
Last Blog:
PHP Function Overloadi...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default Re: C#:Tutorial - Download Data

Until May 1st
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-21-2008, 01:24 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: C#:Tutorial - Download Data

Not enough time to beat Chinmoy (and I don't have the time anyway). What happens after the contest is over? Do I get +rep for useful tutorials?
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-23-2008, 07:27 AM
TcM's Avatar   
TcM TcM is offline
Terminator - I'll be back
 
Join Date: Aug 2006
Location: In a technologic world :p
Posts: 5,709
Rep Power: 47
TcM is a jewel in the roughTcM is a jewel in the roughTcM is a jewel in the rough
Default Re: C#:Tutorial - Download Data

Well... if I did get +rep for half my tutorials... I'd be selling rep points in bulk orders lol.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall


Business Directory | Technology Blog | Windows Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-23-2008, 03:18 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 2,969
Last Blog:
Piano Exam
Rep Power: 25
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: C#:Tutorial - Download Data

Yes, but you write tutorials just for the thrill of it. There's a difference
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 290 06-28-2008 10:10 AM
Need a data analysis program for a project yellowbus General Programming 2 02-13-2008 04:24 PM
Java:Tutorial - Data Types John Java Tutorials 6 07-02-2007 09:16 PM
Fetching Data from a Form Generated Website pclark2 General Programming 5 05-11-2007 06:24 AM


All times are GMT -5. The time now is 03:11 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads