Click here to Skip to main content
Licence 
First Posted 18 Jul 2005
Views 139,546
Downloads 1,080
Bookmarked 41 times

Use HTTPRequest/HTTPResponse to browse WEB pages

Use HTTPRequest/HTTPResponse to browse WEB pages
15 votes, 45.5%
1
4 votes, 12.1%
2
2 votes, 6.1%
3
5 votes, 15.2%
4
7 votes, 21.2%
5
2.12/5 - 33 votes
μ 2.16, σa 2.88 [?]

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

About the Author

Eran Aharonovich

Software Developer

Australia Australia

Member
Been a programmer since 1999.
Experience in:
.Net, C++, C#, VB, VB.NET, ASP, ASP.NET, DLLs, COM etc.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmemberashishkum0:53 22 Dec '10  
GeneralMy vote of 5 PinmemberAlex Fux4:35 30 Jun '10  
GeneralProblem with Russian characters PinmemberAlex Fux12:45 29 Jun '10  
GeneralRe: Problem with Russian characters PinmemberAlex Fux4:30 30 Jun '10  
GeneralMy vote of 2 Pinmembergg679:45 15 Dec '09  
GeneralQuestion Requet form Pinmembernmhai831:30 1 Sep '09  
QuestionHow deal with cookie? Pinmemberlixingyi17:30 10 Mar '09  
GeneralUseful! Pinmembertclin0:05 16 Jan '09  
GeneralMessage Automatically Removed Pinmembercs.it.tech7:46 1 Dec '07  
GeneralGetting Throughput for the Request PinmemberSakthiSurya2:32 23 Feb '07  
QuestionIs it Article? PinmemberRamsu7:59 26 Oct '06  
AnswerRe: Is it Article? Pinmemberantecedents11:45 7 Feb '07  
GeneralRe: Is it Article? PinmemberNirosh10:29 19 Feb '08  
GeneralUse HTTPRequest/HTTPResponse to get data from jsp page Pinmemberparthmankad21:49 15 Sep '06  
GeneralWebCrawlers PinmemberStixoffire220:50 21 Jul '06  
GeneralRe: WebCrawlers PinmemberEran Aharonovich23:24 21 Jul '06  
GeneralWeb Crawler [modified] PinmemberEran Aharonovich4:35 4 Jul '06  
QuestionHelp!!! PinmemberAlexandru Stanciu0:12 11 Jun '06  
QuestionHttRequest Response Problem Pinmemberjay_dubal1:21 26 Mar '06  
AnswerRe: HttRequest Response Problem PinmemberEran Aharonovich7:15 26 Mar '06  
GeneralWhere is the article PinmemberByteGhost1:33 19 Jul '05  
GeneralRe: Where is the article PinmemberEran Aharonovich2:09 19 Jul '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 25 Jul 2005
Article Copyright 2005 by Eran Aharonovich
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid