HTTP GET and HTTP POST in ASP.NET

There are mainly two ways to pass data from one web page to another, using HTTP GET and HTTP POST method.

1)  HTTP GET: In HTTP GET method data passed through url querystring using name value pair. It’s simpler and you can troubleshoot any problems simply by looking at the address bar in your browser because all values passed are displayed there. This is also the primary weakness of this method. The data being passed is visible and is limited in size to the maximum length of a request string.(Maximum length of querystring???  do you know what’s max size??? ….)

For example, http://PatelShailesh.com/get_targetpage.aspx?FirstName=Shailesh&LastName=Patel. In this example, string after ‘?’ is the querystring. You have to seprate querystring name value pair by ‘&’ character.  So in this example there are two querystring field one is ‘FirstName’ and another is ‘LastName’ and values are ‘shailesh’ and ‘patel’ respectively.

Here’s an example of what a web server receives when a client makes a GET request:

GET /get_targetpage.aspx?firstname=shailesh&lastname=patel HTTP/1.1

User-Agent: Mozilla/4.0

Host: www.patelshailesh.com

Here is HTML example how you can pass data using HTTP GET method.

<html>
<head>
<title>Using Http Get</title>
</head>
<body>

<form id=”frm_get” action=”get_targetPage.aspx” target=”_blank” method=”GET” >
<table>

<tr>
<td>First Name: </td>
<td><input type=”text” id=”txtFirstName” name=”firstname” /></td>
</tr>

<tr>
<td>Last Name: </td>
<td><input type=”text” id=”txtLastName” name=”lastname” /></td>
</tr>

<tr>
<td></td>
<td><input type=”submit” value=”Send Using Get” /></td>
</tr>

</table>
</form>
</body>
</html>

When you click on submit button, Page will be redirected get_targetPage.aspx(as stated in action attribute of form element). You will see url like this http://yourcurrentwebsite/get_targetPage.aspx?firstname=valueyouhaveentered&lastname=valueyouhaveentered.  Querystring field like ‘firstname’ and ‘lastname’ are taken from name attribute of input text element.  So DONOT forget to specify name attribute.

If you want to pass value of variable or only some portion of data to another page, you can use response.redirect.  For example, on button click event you can write code like this.

protected void btnSubmit_OnClick(object sender, EventArgs e)
{
string firstNameValue = 'Shailesh';
string URL = "firstName=" + firstNameValue ;
//added firstname variable value in querystring

URL += "&amp;lastName=" + txtLastName.Text ;
// added valued entered in txtlastname textbox in querystring

Response.Write("get_targetPage.aspx?" + URL);
// url will be http://yourcurrentwebsite/get_targetPage.aspx?firstname=Shailesh&amp;lastname=valueyouhaveenteredintxtlastnametextbox

}

Following code to retrieve the data from querystring in target page

string firstName  = Request.QueryString["firstname"].ToString();  // will get 'shailesh'

string lastName = Request.Querystring["lastname"].ToString(); // will get 'patel'

2) HTTP POST:  In HTTP POST request data are embeded in a HTTP HEADER.  So data are NOT visible to end user while you can see the data passed in HTTP GET method.  So if you want to pass sensitive information/data, you should have to use HTTP POST request. Another advantage is that you can send larger amounts of information compare to HTTP GET method.

Here’s an example of what a web server receives when a client makes a POST request:

POST /post_targetpage.aspx HTTP/1.1

Host: www.patelshailesh.com

User-Agent: Mozilla/4.0

Content-Length: 27

Content-Type: application/x-www-form-urlencoded

firstname=shailesh&lastname=patel

Here is HTML example how you can pass data using HTTP POST method.

<html>
<head>
<title>Using Http Post</title>
</head>
<body>

<form id=”frm_post” action=”post_targetPage.aspx” target=”_blank” method=”POST” >

<table>
<tr>
<td>Name 2: </td>
<input type=”text” id=”txtFirstName" name="firstName" />
</td>
</tr>

<tr>
<td>Address 2: </td>
<input type=”text” id=”txtLastName" name=”lastName" />
</td>
</tr>

<tr>
<td></td>
<td><input type=”submit” value=”Send Using Post” /> </td>
</tr>

</table>
</form>
</body>
</html>

When you click on submit button, Page will be redirected post_targetPage.aspx(as stated in action attribute of form element). You will see url like this http://yourcurrentwebsite/post_targetPage.aspx.  Please not here you won’t see any querystring appended in URL since we are making HTTP POST request. Note: You might have noticed that above example is purely HTML code.  In ASP.NET by default all the pages post to themselves. In if you want to make HTTP POST request, you are thinking to change METHOD and ACTION attribute.  But you can’t do that unless you remove ‘runat=server’ attribute which will disable the other features provided in ASP.NET. So you are thinking how you can make HTTP POST request. ASP.NET 2.0 or later version, there is a PostBackUrl property of the ASP.NET control that can submit the page.  PostBackURL property specifies which page to submit data making HTTP POST request.  Here is the syntax <asp:Button ID=”btnSubmit” Runat=server Text=”submit” PostBackUrl=”~/Post_TargetPage.aspx” /> Following code to retrieve the data in target page

string firstName  = Request.Form["firstname"].ToString();  // will get 'shailesh'

string lastName = Request.Form["lastname"].ToString(); // will get 'patel'

Hope this article clears out your doubt between HTTP GET and HTTP POST request.  If this post really helps you, please click the Google +1 button to show it really helps you save your time.  Please leave a comment/question here.

Tagged with:
Posted in Microsoft Technology
18 comments on “HTTP GET and HTTP POST in ASP.NET
  1. Sathish says:

    Thanks for the article.Its good and clears the doubts in the get and post method.

  2. ZAREMA says:

    Thanks the author for article. The main thing do not forget about users, and continue in the same spirit.

  3. gualetar says:

    The subject is fully clear your blog is great.

  4. Indrasena.L says:

    This article is good
    Especially the narration was good understandable manner
    thanks shailesh
    keep post this type of articles

  5. kaitlyn says:

    This was an excellent article, you would think there would be more full examples of HTTP POST specifically. Thankfully, I found this one.

    Keep it up.

  6. Shox NZ says:

    Going to pay attention to your outstanding article, I study it is definitely of the special statements and leading thoughts.Trust that I will certainly get maintain of just what I aspire arriving through your present thoughts.Best wishes with you!

  7. Rajesh Parbat says:

    Super!!!
    Excellent !!!!

  8. Rakesh says:

    This is really good article.
    Helps a lot. Thanks you.

  9. Milind says:

    Really good article, cleared all my doubts.

  10. Krush says:

    how do you POST the data from an ASPX?
    I understand the form from HTML, php or classic ASP,
    But in ASP.NET, you already have a FORM runat=server, So you can’t add forms in it.
    How would I do it from the ASPX?

  11. Shailesh says:

    @Krush
    When you look at the .aspx page, you won’t see action and method attributes of form element. By default form uses HTTP Post method and value of “action” attribute is that page itself. If you look at the view source of any .aspx page in browser, you will see those values generated by ASP.NET.

    ASP.NET uses HTTP Post by default for .aspx and form is posted to itself by default.

  12. kiran says:

    Wonderful article shailesh

  13. Mohanraj says:

    if you are using postbock URL in the submit button, the value will not visible in the Header.

    I hope here post method will not give any impact.

  14. cunnyTeenia says:

    ipage web hosting guide

  15. ritesh says:

    How can i use http post and get method on .aspx page? Please let me know with the example.

  16. Lakshya says:

    it is a gud article.

  17. mahmoud says:

    hey you
    you saved my time a lot
    thanks
    i migrated from asp classic to asp.net and was so confused about get a post method,
    thanks a million

  18. Hasitha says:

    Thankx Buddy,
    Thank you somuch

2 Pings/Trackbacks for "HTTP GET and HTTP POST in ASP.NET"
  1. [...] HTTP Post (passing information in HTTP header) For more detailed information about HTTP GET and HTTP POST, please visit this link. [...]

Ads
OutbackDirect.co.uk is a trading site of World of Power.co.uk Limited. Outback Direct is the official Outback Consumer Warranty, Parts and Accessory Partners for the UK. We currently hold in stock 1000’s of parts and accessories for current and older Outback barbecues. We endeavour to ensure rapid and secure product delivery the length and breadth of mainland United Kingdom, this factor coupled with our excellent customer service has led to the market position we currently enjoy.
outback barbecue
omega 300 gas barbecue
Omega 250 Gas Hooded BBQ
------------------------------------------------------------ Silver Sky Imports is a unique online marketplace that offers unique handicrafts from Tibet and Nepal. Launched in 2003, Silver Sky Imports is currently the largest supplier of handmade items from the Himalayas. We specialize in the largest variety of Tibetan Singing Bowls and Crystal Singing Bowls online and include individual sound samples, pictures and descriptions for each bowl. Some of our other products include Tibetan Prayer Flags, Tingshas, Bell and Dorje, Incense, Felted Wool Handbags, Wood Masks, Prayer Wheels, Gongs, Bronze Statues, and Tibetan Meditation Rugs. monks
------------------------------------------------------------ Airpush, a mobile ad network that is only two years old, has quickly grown to one with over 5,000 advertisers. One thing they've heard from that sea of advertisers over the past 12 months, and especially over the past six, is that real-time bidding (RTB) capabilities are now a must. To answer those needs, the company today unveiled AirDSP, a platform meant to easily connect advertisers to RTB exchanges. Read more: Mobile RTB
------------------------------------------------------------ Danish site about electronic cigarettes E Cigarette, Electronic Cigarette and E Cigarettes e cigaret
------------------------------------------------------------ DrugRisks.com drugrisk​ recall ​informat​ion​ drugrisk​ yaz​
------------------------------------------------------------