Techie Talks

Wednesday, March 01, 2006

Read a webpage as a string

private string WebPageFromURL(string url)
{
WebRequest myWebRequest = WebRequest.Create(url);

// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse();

// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myWebResponse.GetResponseStream();

System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

// Pipe the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(ReceiveStream, encode);
string readStr = readStream.ReadToEnd();
readStream.Close();
ReceiveStream.Close();
myWebResponse.Close();

return readStr;
}

0 Comments:

Post a Comment

<< Home