Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 143 - Difference between http get and http post methods

Suggested Videos
Part 140 - Creating an image gallery using asp.net and c#
Part 141 - Contact us page using asp.net and c#
Part 142 - Contact us page using asp.net and c# continued



HTTP stands for Hypertext Transfer Protocol. Http is the most common protocol used for communication between a web server and a client. For example, to request home page of Pragim Technologies, you would type the following URL. Notice that the URL has http prefix.
http://pragimtech.com/Home.aspx

The 2 most commonly used HTTP methods are GET and POST.



Let's uderstand GET and POST requests with an example. 
1. Create an asp.net web application

2. Set the name of the project to WebFormsDemo

3. Add WebForm1.aspx. Copy and paste the following HTML
<div style="font-family:Arial">
<table>
    <tr>
        <td>First Name
        </td>
        <td>
            :<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Last Name
        </td>
        <td>
            :<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Email
        </td>
        <td>
            :<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>
<asp:Button ID="Button1" runat="server" Text="Submit" 
    onclick="Button1_Click" />
</div>

4. Copy and paste the following code in WebForm1.aspx.cs
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/WebForm2.aspx?FirstName=" + txtFirstName.Text +
            "&LastName=" + txtLastName.Text + "&Email=" + txtEmail.Text);
    }
}

5. Add WebForm2.aspx. Copy and paste the following HTML
<div style="font-family:Arial">
    <table>
    <tr>
        <td>First Name
        </td>
        <td>
            :<asp:Label ID="lblFirstName" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            Last Name
        </td>
        <td>
            :<asp:Label ID="lblLastName" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            Email
        </td>
        <td>
            :<asp:Label ID="lblEmail" runat="server"></asp:Label>
        </td>
    </tr>
</table>
</div>

6. Copy and paste the following code in WebForm2.aspx.cs
public partial class WebForm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblFirstName.Text = Request.QueryString["FirstName"];
        lblLastName.Text = Request.QueryString["LastName"];
        lblEmail.Text = Request.QueryString["Email"];
    }
}

Download Fiddler tool from http://fiddler2.com/get-fiddler and run it.

Build the solution. Open a browser window. In the Address bar type the following address and press and enter key.
http://localhost/WebFormsDemo/WebForm1.aspx

So here we are issuing a GET request for WebForm1.aspx. In the fiddler tool, you can clearly notice that a GET request is issued for WebForm1.aspx

At this point, you will have the user interface to enter FirstName, LastName and Email. Enter the details and when you click "Submit" button, the data will be posted to the server. So, here we are issuing a POST request to submit the data on WebForm1.aspx. In the fiddler tool, you can clearly notice that a POST request is issued to submit WebForm1 data. 

In the Button1_Click() event handler, we have Response.Redirect() call, which issues a GET request to WebForm2.aspx

GET Request - is generally used to get data from the web server. A GET request is generally issued,
1. When you click on a hyperlink
2. When Response.Redirect() statement is executed
3. When you type URL in the address bar and hit enter

POST Request - is generally used to submit data to the server. A POST request is generally issued,
1. When you click on a submit button
2. When AUTOPOST back is set true and when a selection in the DropDownList is changed

Difference between GET and POST method
1. GET method appends data to the URL, where as with the POST method data can either be appended to the URL or in the message body.
2. As GET request rely on querystrings to send data to the server, there is a length restriction, where as POST requests have no restrictions on data length.
3. While it is possible to change the state of data in database using GET request, they should only be used to retrieve data.

In asp.net webforms, IsPostBack page property is used to check if the request is a GET or a POST request. If IsPostBack property returns true, then the request is POST, else the request is GET.

2 comments:

  1. how do we stop fiddler to fiddle with the posted form data in asp.net

    ReplyDelete
  2. is it possible to do html form post from c# ?

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.