To
work with any of these samples, you will need the
following: .........................................Microsoft
.NET SDK
In this article, we will see how to
make HTTP GET and POST requests on Web Pages.
The .NET framework uses the System.Net.WebRequest
and the System.Net.WebResponse
classes to make requests and get
responses.
The WebRequest
class is used to send a request to a
particular Uniform Resource Identifier (URI), such as a Web page from a server. Applications can use the
WebRequest class to handle requests in a protocol-agnostic manner.
Applications should never create WebRequest
objects directly; instead they should be created with the
WebRequestFactory class.
The System.Net.WebRequestFactory
class's Create()
method is used to instantiate WebRequest
objects. The specific class of object returned is based on the URI scheme passed to the
Create() method. The following example creates a
WebRequest instance for an HTTP request. The actual instance returned is an instance of
HttpWebRequest.
Applications can use the WebResponse
class to handle responses in a protocol-agnostic manner. The
WebResponse class can be used to access any resource on the network that is addressable with a Uniform Resource Identifier (URI), such as
http://www.icommware.com/.
Client applications should never create WebResponse
objects directly, they should be created by calling the
GetResponse() method on a
WebRequest instance.
I will illustrate this by developing an ASP+ page that makes GET and POST
requests over the Web and retrieves web pages. The Initial UI to this page looks
like this.
Please enter a URL :
Select a URL to view:
When the user types in a URL on the
"Please enter a URL" Edit box and presses on the associated Submit
button, a GET request is made for the web page in response to the POST of the
form. However, when the user selects something from the dropdown list box and
clicks on the associated Submit button, a POST request is made on the Web Page
in response to the POST of the form.
<!------------------------------------------------------------------------------------------------> <!-- @Article Making GET and POST requests on Web Pages with C#, ASP+ and the .NET Framework --> <!-- @Copyright (c) 2000, Gopalan Suresh Raj. All Rights Reserved
--> <!------------------------------------------------------------------------------------------------>
<html> <head> <title> Making GET and POST requests on Web Pages with C#, ASP+ and the .NET Framework by Gopalan Suresh Raj </title> </head>
<body>
<SCRIPT runat="server">
/***************************************************** * writeToURL is a method that writes the contents of * a specified URL to the web *****************************************************/ void writeToURL (WebRequest request, string data)
{
byte [] bytes = null; // Get the data that is being posted (or sent) to the server bytes = System.Text.Encoding.ASCII.GetBytes (data); request.ContentLength = bytes.Length; // 1. Get an output stream from the request object Stream outputStream = request.GetRequestStream ();
// 2. Post the data out to the stream outputStream.Write (bytes, 0, bytes.Length);
// 3. Close the output stream and send the data out to the web server outputStream.Close (); } // end writeToURL method
/***************************************************** * retrieveFromURL is a method that retrieves the contents of * a specified URL in response to a request *****************************************************/ String retrieveFromURL (WebRequest request) { // 1. Get the Web Response Object from the request WebResponse response = request.GetResponse(); // 2. Get the Stream Object from the response Stream responseStream = response.GetResponseStream();
// 3. Create a stream reader and associate it with the stream object StreamReader reader = new StreamReader (responseStream);
// 4. read the entire stream return reader.ReadToEnd (); }// end
retrieveFromURL method
/***************************************************** * postToURL is a method that forces a POST * of data to a specified URL * * A HTTP POST is a combination of a write to the Web Server * and an immediate read from the Web server *****************************************************/ void postToURL (Object sender, EventArgs eventArgs)
{
string value = urlList.SelectedItem.Value; // Create the Web Request Object WebRequest request = WebRequestFactory.Create (value); // Specify that you want to POST data request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; if (value != null) { // write out the data to the web server writeToURL (request, value); } else { request.ContentLength = 0; } // read the response from the Web Server string htmlContent = retrieveFromURL (request); // Display the content on the web page htmlDisplayArea.InnerHtml = ""; if (htmlContent != null) { htmlDisplayArea.InnerHtml += htmlContent; } // end if statement } // end postToURL method
/***************************************************** * getFromURL is a method that retrieves the contents of * a specified URL in response to a request *****************************************************/ void getFromURL (Object sender, EventArgs eventArgs)
{
// 1. Create the Web Request Object WebRequest request = WebRequestFactory.Create (urlEditBox.Text); string htmlContent = retrieveFromURL (request);
// 2. Display the content on the web page htmlDisplayArea.InnerHtml = ""; if (htmlContent != null) { htmlDisplayArea.InnerHtml += htmlContent; } // end if statement } // end getURL
</SCRIPT>
<!---------------------------------------------------- The Form code
is here ------------------------------------------------------> <center>
<form method="post" action="webforays.aspx" runat="server"> Please enter a URL : <asp:TextBox Rows="1" Cols="80" runat=server ID="urlEditBox"> </asp:TextBox>
<asp:Button runat=server Text="Submit for HTTP GET" ID="getURL" onClick="getFromURL"> </asp:button> <br>
<br> Select a URL to view: <asp:dropdownlist runat=server ID="urlList"> <asp:listitem>http://www.execpc.com/~gopalan</asp:listitem> <asp:listitem>http://www14.brinkster.com/ecommware</asp:listitem> <asp:listitem>http://www.icommware.com</asp:listitem> </asp:dropdownlist>
<asp:Button runat=server Text="Submit for HTTP POST" ID="postURL" onClick="postToURL"> </asp:button>
2. Deploy it on Microsoft Internet Information
Server (IIS)
Deploy the web page on the Web Server by copying
it to a folder under your Web Root directory. I have copied it on my machine on
to C:\Inetpub\wwwroot\webforays.
So when I open up IE and type in