Sponsored Ad

Wednesday, October 21, 2009

C# http

HTTP is the main transport mechanism for communicating with the resources on the World Wide Web. A developer often want to get the websites for various reasons including: engine-caching search page, obtaining information on a particular page, or even the browser application of similar ability. To help with this task, the. NET Framework includes classes that make it so easy.

HTTP classes in the framework. NET HttpWebRequest and HttpWebResponse. The steps required to specify a website for a HttpWebRequest object, making the actual request, and use the HttpWebResponse object to receive the page. Thereafter, you must use flow operations to extract information from the page. Listing 1 shows how this process works.

using System;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Fetches a Web Page
/// </summary>
class WebFetch
{
    static void Main(string[] args)
    {
        // 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
        HttpWebRequest  request  = (HttpWebRequest)
            WebRequest.Create

        // execute the request
        HttpWebResponse response = (HttpWebResponse)
            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); // any more data to read?

        // print out page source
        Console.WriteLine(sb.ToString());
    }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers