Andrew Beaton :: FAQ

Tag: WebResponse

Read source of a web page in C#

by Andrew on Nov.13, 2009, under C#, Programming

This example shows how to read the source of a web page in C#.

// used to build entire input
StringBuilder sb = new StringBuilder();
 
// used on each read operation
byte[] buf = new byte[8192];
 
// prepare the web page we will be asking for
WebRequest request  = WebRequest.Create("http://andrewbeaton.com");
 
// execute the request
WebResponse response = request.GetResponse();
 
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
 
string tempString = null;
int count = 0;
 
do
{
    // fill the buffer with data
    count = resStream.Read(buf, 0, buf.Length);
 
    // make sure we read some data
    if (count != 0) 
    {
        // translate from bytes to ASCII text
        tempString = Encoding.ASCII.GetString(buf, 0, count);
 
        // continue building the string
       sb.Append(tempString);
    }
}
while (count > 0);
Leave a Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

 

Archives

All entries, chronologically...