Skip to main content

Difference between HttpGet and HttpPost Method

The Hypertext Transfer Protocol (HTTP) is a communication protocol that is designed to enable request-response between clients and servers. Here, a web browser is the client and an application on a computer that hosts a web site is the server

Http GET method
Take an html form named "get_form.htm" and write the following code.
  1. <html>
  2. <head>
  3. <title>Using Http Get Method</title>
  4. </head>
  5. <body>
  6. <form id="frm_get" action=" Receiving_Get_Form.aspx" target="_blank" method="GET" >
  7. <table>
  8. <tr>
  9. <td>First Name : </td> <td><input type="text" id="txtF_Name" name="F_name" /></td>
  10. </tr> <tr>
  11. <td>Last Name : </td> <td><input type=" text" id="txtL_name" name="L_name" /></td>
  12. </tr> <tr>
  13. <td>Email-Id : </td> <td><input type="text" id="txtE_mail" name="E_mail" /></td>
  14. </tr> <tr>
  15. <td>Password: </td> <td><input type="password" id="txtP_word" name="P_word"/> </td>
  16. </tr> <tr>
  17. <td><input type="submit" value="Submit" /></td>
  18. </tr>
  19. </table>
  20. </form> </body>
  21. </html>
When we click submit button of this form, it will be redirected to "Receiving_Get_Form.aspx" in a new window.

Page.Request.QueryString["param_name"]

  1. <%@ page language="C#" AutoEventWireup="true" codeFile="Receiving_ Get_Form.aspx.cs" Inherits="Receiving_ Get_Form"% >
  2. <html>
  3. <head>
  4. <title>Data Received Here </title>
  5. </head>
  6. <body>
  7. <table border="1" cellpadding="6" cellspacing="3" >
  8. <tr>
  9. <td>First Name : </td> <td> <% Response.Write(Page.Request.QueryString["F_name"]); %> </td>
  10. </tr>
  11. <tr>
  12. <td>Last Name : </td> <td> <% Response.Write(Page.Request.QueryString["L_name"]); %> </td>
  13. </tr>
  14. <tr>
  15. <td>Email-Id : </td> <td> <% Response.Write(Page.Request.QueryString["E_mail"]); %> </td>
  16. </tr>
  17. <tr>
  18. <td>Password : </td> <td> <% Response.Write(Page.Request.QueryString["P_word"]); %> </td>
  19. </tr>
  20. </table>
  21. </body>
  22. </html>
The First Name, Last Name, Email-Id and Password text boxes of get_form.htm form will be sent as parameter of query string with the field name are F_name, F_name, E_mail and P_word respectively. The name of query string fields automatically taken from name attribute of each HTML element, so don’t forget to specify the name attribute So that values should be retrieving using Page.Request.QueryString ["param_name"] here in "Receiving_Get_Form.aspx" automatically.

Key points about data submitted by using HttpGet

  1. GET - Requests data from a specified resource
  2. An hyperlink or anchor tag that points to an action will ALWAYS be an HttpGet.
  3. Data is submitted as a part of url.
  4. Data is visible to the user as it posts as query string.
  5. It is not secure but fast and quick.
  6. It use Stack method for passing form variable.
  7. Data is limited to max length of query string.
  8. It is good when you want user to bookmark page.

HttpPost method

The POST request method is designed to request that a web server accepts the data enclosed in the request message's body for storage
Take an html form named “post_form.htm" and write the following code.
  1. </head>
  2. <body>
  3. <form id="frm_post" action=" Receiving_Post_Form.aspx" target="_blank" method=" POST" >
  4. <table>
  5. <tr>
  6. <td>First Name : </td> <td><input type="text" id="txtF_Name" name="F_name" /></td>
  7. </tr> <tr>
  8. <td>Last Name : </td> <td><input type=" text" id="txtL_name" name="L_name" /></td>
  9. </tr> <tr>
  10. <td>Email-Id : </td> <td><input type="text" id="txtE_mail" name="E_mail" /></td>
  11. </tr> <tr>
  12. <td>Password: </td> <td><input type="password" id="txtP_word" name="P_word"/> </td>
  13. </tr> <tr>
  14. <td><input type="submit" value="Submit" /></td>
  15. </tr>
  16. </table>
  17. </form> </body>
  18. </html>
When we click submit button of this form, it will be redirected to "Receiving_Post_Form.aspx" (opened in new window too). In ASP.NET, if data passed through HTTP Post method we need the following code to retrieve the data (as written in "Receiving_Post_Form.aspx").

Page.Request.Form["param_name"]

  1. <%@ page language="C#" AutoEventWireup="true" codeFile="Receiving_Post_Form.aspx.cs" Inherits=" Receiving_Post_Form"% >
  2. <html>
  3. <head>
  4. <title>Data Received Here </title>
  5. </head>
  6. <body>
  7. <table border="1" cellpadding="6" cellspacing="3" >
  8. <tr>
  9. <td>First Name : </td> <td> <% Response.Write(Page.Request.Form["F_name"]); %> </td>
  10. </tr>
  11. <tr>
  12. <td>Last Name : </td> <td> <% Response.Write(Page.Request.Form["L_name"]); %> </td>
  13. </tr>
  14. <tr>
  15. <td>Email-Id : </td> <td> <% Response.Write(Page.Request. Form["E_mail"]); %> </td>
  16. </tr>
  17. <tr>
  18. <td>Password : </td> <td> <% Response.Write(Page.Request. Form["P_word"]); %> </td>
  19. </tr>
  20. </table>
  21. </body>
  22. </html>
Values of "post_form.htm"(that used method="POST") form sent using POST method therefore, the URL still intact, we retrieve The First Name, Last Name, Email-Id and Password text using Page.Request.Form["param_name"] value taken from the name each of HTML element of second form (Once again don’t forget to specify name attribute for all textboxes so that we are able to get value automatically here in this page). When you click Submit button, values of "post_form.htm" passed to "Receiving_Post_Form.aspx".
Key points about data submitted using HttpPost
  1. POST - Submits data to be processed to a specified resource
  2. A Submit button will always initiate an HttpPost request.
  3. Data is submitted in http request body.
  4. Data is not visible in the url.
  5. It is more secured but slower as compared to GET.
  6. It use heap method for passing form variable
  7. It can post unlimited form variables.
  8. It is advisable for sending critical data which should not visible to users.

Comments

Popular posts from this blog

gcAllowVeryLargeObjects Element

There are numerous new features coming with .NET 4.5 and here, on this blog, you can find several posts about it. But the feature we are goint to talk about today is very exciting, because we were waiting for it more than 10 years. Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio 2012, and create C# Console Application, like picture below. First lets create simple struct with two double members like example below: 1 2 3 4 5 6 7 8 9 10 11 12 public struct ComplexNumber {      public double Re;      public double Im;      public ComplexNumber( double re, double im)      {    ...

Support for debugging lambda expressions with Visual Studio 2015

Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.  With visual studio 2015 Microsoft has added support for debugging lambda expressions. Let’s first look at an example, and then I’ll walk you through current limitations. Example To try this yourself, create a new C# Console app with this code: using System.Diagnostics; using System.Linq; class Program { static void Main() { float[] values = Enumerable.Range(0, 100).Select(i => (float)i / 10).ToArray(); Debugger.Break(); } } Then compile, start debugging, and add “values.Where(v => (int)v == 3).ToArray()” in the Watch window. You’ll be happy to see the same as what the screenshot above shows you. I am using Visual Studio 2015 Preview and it has some limitati...

How to allow a very large object in .net application?

Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio, and create C# Console Application. Lets create simple struct with two double members like example below: public struct ComplexNumber { public double Re; public double Im; public ComplexNumber(double re, double im) { Re = re; Im = im; } } As we know this structure consumes about 16 bytes of memory. So if we want to create array of this type which consume more than 2GB we need to create array at least with 134217728 instances. So this sample program below creates 130000000 (about 1,97 GB) of array. int maxCount = 130000000; ComplexNumber[] arr = null; try { arr = new ComplexNumber[maxCount]; } catch (Exception ex) { Console.WriteLine(ex.Message); } So if we run t...