Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Use HTTPRequest/HTTPResponse to browse WEB pages

0.00/5 (No votes)
25 Jul 2005 1  
Use HTTPRequest/HTTPResponse to browse WEB pages

Introduction

This article is about demonstrating how to simulate a browser request to any web page.

Can be used:

1. When we want to get the content of a web page, the HTML content automatically like when developing a web crawler or something like this.

2. When we want a certain service which will accept parameters and return information in return.

The code below should be used with ASP pages.

The ASP page will receive 2 form parameters using the POST method and return information in return.

Getting Started

Create a .NET Web Project called DEMO ( or anything else ).
Visual Studio will create the WebForm.aspx file automatically.

In the code behind section, under Page_Load event you can check the Request.Form collection and us Response.Write to write back information to requesting client.

Additional explanation is documented in the code segment.

Good luck.

 

 

using System;

using System.Net;

using System.IO;

using System.Text;

 

namespace HttpRequest

{

      /// <summary>

      /// Summary description for Class1.

      /// </summary>

      class Class1

      {

           

            /// <summary>

            /// The main entry point for the application.

            /// </summary>

            [STAThread]

            static void Main(string[] args)

            {

                 

                  string param1 = "value1";

                  string param2 = "value2";

 

                  ASCIIEncoding encoding = new ASCIIEncoding();

                 

                  string postData = string.Format("param1={0}&param2={1}", param1, param2 );

 

                  byte[]  buffer = encoding.GetBytes( postData );

 

                  // Prepare web request...

                  HttpWebRequest myRequest =

                        (HttpWebRequest)WebRequest.Create("http://localhost/DEMO/WebForm1.aspx");

 

                  // We use POST ( we can also use GET )

                  myRequest.Method = "POST";

 

                  // Set the content type to a FORM

                  myRequest.ContentType ="application/x-www-form-urlencoded";

 

                  // Get length of content

                  myRequest.ContentLength = buffer.Length;

 

                  // Get request stream

                  Stream newStream = myRequest.GetRequestStream();

                 

                  // Send the data.

                  newStream.Write(buffer,0,buffer.Length);

 

                  // Close stream

                  newStream.Close();

 

 

 

                  // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.

                  HttpWebResponse myHttpWebResponse= (HttpWebResponse)myRequest.GetResponse();

 

                  // Display the contents of the page to the console.

                  Stream streamResponse=myHttpWebResponse.GetResponseStream();

 

                  // Get stream object

                  StreamReader streamRead = new StreamReader( streamResponse );

 

                  Char[] readBuffer = new Char[256];

 

                  // Read from buffer

                  int count = streamRead.Read( readBuffer, 0, 256 );

 

                  while (count > 0)

                  {

                        // get string

                        String resultData = new String( readBuffer, 0, count);

 

                        // Write the data

                        Console.WriteLine( resultData );

 

                        // Read from buffer

                        count = streamRead.Read( readBuffer, 0, 256);

                  }

                 

                  // Release the response object resources.

                  streamRead.Close();

                  streamResponse.Close();

                 

                  // Close response

                  myHttpWebResponse.Close();

                 

                 

            }

      }

}

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here