Making GET and POST Requests on Web Pages 

using C#, ASP+, and The .NET Framework

Gopalan Suresh Raj

Note
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.

WebRequest wr = WebRequestFactory.Create("http://www.icommware.com/");

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.

Develop the ASP+ Web Page

1. Develop The ASP+ Page : webforays.aspx
.

webforays.aspx
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
<!------------------------------------------------------------------------------------------------>
<!--  @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                              -->
<!------------------------------------------------------------------------------------------------>

<%@ Page language="C#"  %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Assembly Name="System.Net.dll" %>
<%@ Assembly Name="System.Text.RegularExpressions.dll" %>


<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>
    
     <frame runat=server ID="htmlDisplayArea" MaintainState="false"  Wrap="True">
     </frame>

    </center>
  
   </form>

  </body>
</html>

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 

http://localhost/webforays/webforays.aspx 

I get the web page displayed.

Download the entire source code as a zip file.

 

click here to go to
My Advanced C#/.NET Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He is contributing author to a couple of books "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia© site (http://www.execpc.com/~gopalan) or mail him at gopalan@execpc.com.

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since November 1, 2000.

Last Updated : Nov 1, '00

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2000, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.