+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 14

Thread: C#:Tutorial - Download Data

  1. #1
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    Re: C#:Tutorial - Download Data

    Very nice tutorial!

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: C#:Tutorial - Download Data

    Great tutorial. 30 points awarded.

  4. #4
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  5. #5
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: C#:Tutorial - Download Data

    While the contest lasts, yes.

  6. #6
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  7. #7
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    Re: C#:Tutorial - Download Data

    Until May 1st

  8. #8
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  9. #9
    TcM
    TcM is offline
    Code Warrior TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM is a name known to all TcM's Avatar
    Join Date
    Aug 2006
    Posts
    11,461
    Blog Entries
    6

    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.

  10. #10
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Project: ionFiles - Joomla Simple File Download
    By Jordan in forum Community Projects
    Replies: 369
    Last Post: 01-30-2010, 01:10 PM
  2. Need a data analysis program for a project
    By yellowbus in forum General Programming
    Replies: 2
    Last Post: 02-13-2008, 04:24 PM
  3. Java:Tutorial - Data Types
    By John in forum Java Tutorials
    Replies: 6
    Last Post: 07-02-2007, 09:16 PM
  4. Fetching Data from a Form Generated Website
    By pclark2 in forum General Programming
    Replies: 5
    Last Post: 05-11-2007, 06:24 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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