
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
/// <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;
}
}-Offline- |