+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: C#:Tutorial - Download Data

  1. #1
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: C#:Tutorial - Download Data

    Very nice tutorial!

  4. #3
    Jordan Guest

    Re: C#:Tutorial - Download Data

    Great tutorial. 30 points awarded.

  5. #4
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C#:Tutorial - Download Data

    Great! Do I get points every time a write a useful tutorial, then?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #5
    Jordan Guest

    Re: C#:Tutorial - Download Data

    While the contest lasts, yes.

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C#:Tutorial - Download Data

    Oh - how long does the contest last?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #7
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: C#:Tutorial - Download Data

    Until May 1st

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    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?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    Join Date
    Aug 2006
    Posts
    11,209
    Blog Entries
    6
    Rep Power
    101

    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.

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C#:Tutorial - Download Data

    Yes, but you write tutorials just for the thrill of it. There's a difference

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2011, 03:16 PM
  2. Data Masking helps in reducing data privacy violations
    By tossy in forum Software Security
    Replies: 1
    Last Post: 07-06-2009, 05:52 PM
  3. Java:Tutorial - Data Types
    By John in forum Java Tutorials
    Replies: 6
    Last Post: 07-02-2007, 07:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts