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.
- <html>
- <head>
- <title>Using Http Get Method</title>
- </head>
- <body>
- <form id="frm_get" action=" Receiving_Get_Form.aspx" target="_blank" method="GET" >
- <table>
- <tr>
- <td>First Name : </td> <td><input type="text" id="txtF_Name" name="F_name" /></td>
- </tr> <tr>
- <td>Last Name : </td> <td><input type=" text" id="txtL_name" name="L_name" /></td>
- </tr> <tr>
- <td>Email-Id : </td> <td><input type="text" id="txtE_mail" name="E_mail" /></td>
- </tr> <tr>
- <td>Password: </td> <td><input type="password" id="txtP_word" name="P_word"/> </td>
- </tr> <tr>
- <td><input type="submit" value="Submit" /></td>
- </tr>
- </table>
- </form> </body>
- </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"]
- <%@ page language="C#" AutoEventWireup="true" codeFile="Receiving_ Get_Form.aspx.cs" Inherits="Receiving_ Get_Form"% >
- <html>
- <head>
- <title>Data Received Here </title>
- </head>
- <body>
- <table border="1" cellpadding="6" cellspacing="3" >
- <tr>
- <td>First Name : </td> <td> <% Response.Write(Page.Request.QueryString["F_name"]); %> </td>
- </tr>
- <tr>
- <td>Last Name : </td> <td> <% Response.Write(Page.Request.QueryString["L_name"]); %> </td>
- </tr>
- <tr>
- <td>Email-Id : </td> <td> <% Response.Write(Page.Request.QueryString["E_mail"]); %> </td>
- </tr>
- <tr>
- <td>Password : </td> <td> <% Response.Write(Page.Request.QueryString["P_word"]); %> </td>
- </tr>
- </table>
- </body>
- </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
- GET - Requests data from a specified resource
- An hyperlink or anchor tag that points to an action will ALWAYS be an HttpGet.
- Data is submitted as a part of url.
- Data is visible to the user as it posts as query string.
- It is not secure but fast and quick.
- It use Stack method for passing form variable.
- Data is limited to max length of query string.
- 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.
- </head>
- <body>
- <form id="frm_post" action=" Receiving_Post_Form.aspx" target="_blank" method=" POST" >
- <table>
- <tr>
- <td>First Name : </td> <td><input type="text" id="txtF_Name" name="F_name" /></td>
- </tr> <tr>
- <td>Last Name : </td> <td><input type=" text" id="txtL_name" name="L_name" /></td>
- </tr> <tr>
- <td>Email-Id : </td> <td><input type="text" id="txtE_mail" name="E_mail" /></td>
- </tr> <tr>
- <td>Password: </td> <td><input type="password" id="txtP_word" name="P_word"/> </td>
- </tr> <tr>
- <td><input type="submit" value="Submit" /></td>
- </tr>
- </table>
- </form> </body>
- </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"]
- <%@ page language="C#" AutoEventWireup="true" codeFile="Receiving_Post_Form.aspx.cs" Inherits=" Receiving_Post_Form"% >
- <html>
- <head>
- <title>Data Received Here </title>
- </head>
- <body>
- <table border="1" cellpadding="6" cellspacing="3" >
- <tr>
- <td>First Name : </td> <td> <% Response.Write(Page.Request.Form["F_name"]); %> </td>
- </tr>
- <tr>
- <td>Last Name : </td> <td> <% Response.Write(Page.Request.Form["L_name"]); %> </td>
- </tr>
- <tr>
- <td>Email-Id : </td> <td> <% Response.Write(Page.Request. Form["E_mail"]); %> </td>
- </tr>
- <tr>
- <td>Password : </td> <td> <% Response.Write(Page.Request. Form["P_word"]); %> </td>
- </tr>
- </table>
- </body>
- </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
- POST - Submits data to be processed to a specified resource
- A Submit button will always initiate an HttpPost request.
- Data is submitted in http request body.
- Data is not visible in the url.
- It is more secured but slower as compared to GET.
- It use heap method for passing form variable
- It can post unlimited form variables.
- It is advisable for sending critical data which should not visible to users.
When we develop web applications we always have to deal with the HTML.
Today we will see the difference between the HTTPGet and HTTPPost attributes of ASP.NETMVC.
These attributes encode request parameters as name-and-value pairs in the HTTP request. The HTTP-GET protocol and the HTTP-POST protocol provide backward compatibility in the following ways.
* For example, you may want two versions of the Edit method, one that renders the edit form and the other that handles the request when that form is posted:
[HttpGet]
public ActionResult Edit(string id)
{
return View();
}
[HttpPost]
public ActionResult Edit(string id, FormCollection form)
{
//Save the item and redirect…}
When a POST request for /home/edit is received, the action invoker creates a list of all methods of the Controller that match the edit action name. In this case, you would end up with a list of two methods.
Afterward, the invoker looks at all of the ActionSelectorAttribute instances applied to
each method and calls the IsValidForRequest method on each. If each attribute returns true, then the method is considered valid for the current action.
each method and calls the IsValidForRequest method on each. If each attribute returns true, then the method is considered valid for the current action.
For example, in this case, when you ask the first method if it can handle a POST request, it will respond with false because it only handles GET requests.
The second method responds with true because it can handle the POST request, and it is the one selected to handle the action.
If no method is found that meets these criteria, the invoker will call theHandleUnknownAction method on the Controller, supplying the name of the missing action. If more than one action method meeting these criteria is found, an InvalidOperationExceptionis thrown.
* Difference between HTTPGet and HTTPPost methods –
Fundamental Difference is probably the Visibility -
The HTTPGet protocol creates a query string of the name-and-value pairs and then appends the query string to the URL of the script on the server that handles the request. Therefore, you can mark the request.
The HTTPPost protocol passes the name-and-value pairs in the body of the HTTP request message.
Length -
Since, HTTPGet request goes via URL, so it has a limitation for its length. It can’t be more than 255 characters long (though this is browser dependent, but usually the max is 255 characters only).
Whereas
No such maximum length limitation holds for the HTTPPost request for the obvious reason that it becomes a part of the body of the HTTP request and there is no size limitation for the body of an HTTP request/response.
Performance -
HTTPGet request is comparatively faster as it’s relatively simpler to create a GET request and
the time spent in the encapsulation of the HTTPPost request in the HTTP body is saved in this case. In addition, the maximum length restriction facilitates better optimization of HTTPGet implementation.
Type of Data -
HTTPGet request is sent via URL string and as we all know that URL can be text-only, so GET can carry only text data
whereas
HTTPPost has no such restriction and it can carry both text as well as binary data.
Caching/Bookmarking – again for the obvious reason that a HTTPGet request is nothing but an URL hence it can be cached as well as Bookmarked.
No such luxuries with a HTTPPost request.
FORM Default -
HTTPGet is the default method of the HTML FORM element.
To submit a FORM using POST method, we need to specify the method attribute and give it the value “HTTPPost”.
For more on ASP.NET MVC visit http://microsoftmentalist.wordpress.com/asp-net-2/
No comments:
Post a Comment