friendsterTalk - Friendster Forum

friendsterTalk - Friendster Forum

Welcome guest! Please Login or Register.

#1  2008-11-11 17:22:22

creamownedz
 active server protocol
creamownedz's display avatar
» FriendsterTalker
PsychoCoder
Class-S
Location: Ftalk & MCTF
Registered: 2008-03-22
Posts: 115
Last visit: Yesterday
Reputation: 7

Retrieve data from a URL [c#]

This is a snippet that will take a valid URL and return all the information from that URL


Instructions: You will need a reference to the following Namespaces

System
System.IO
System.Net
System.Text


Code:

/// <summary>
/// method for retrieving the data from the provided URL
/// </summary>
/// <param name="url">URL we're scraping</param>
/// <returns></returns>
public string LoadSiteContents(string url)
{
    try
    {
        //create a new WebRequest object
        WebRequest request = WebRequest.Create(url);

        //create StreamReader to hold the returned request
        StreamReader stream = new StreamReader(request.GetResponse().GetResponseStream());

        //StringBuilder to hold info from the request
        StringBuilder builder = new StringBuilder();

        //now loop through the response
        while (!(stream.Peek() == 0))
        {
            //now make sure we're not looking at a blank line
            if(stream.ReadLine().Length>0) builder.Append(stream.ReadLine());
        }

        //close up the StreamReader
        stream.Close();

        //return the information
        return builder.ToString();
    }
    catch (Exception ex)
    {
        //put your error handling here
        return string.Empty;
    }
}

...Giving of reputation are highly appreciating for me...
Computers don't make errors what they do they do on purpose
...computer is my expertise...
....an addicted coder....
....don't ripp all my tutorials....

Search Friendstertalk

Board footer

FriendsterTalk is not affiliated with Friendster.com
Copyright © 2002–2009 PunBB

[ 8 queries - 12.794 second ]

Pay Per Click Ads by pay per click advertising by Kontera

FriendsterTalk.com x

Welcome to FriendsterTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)