Monday, November 10, 2008

Post method of form data handling by request object

In POST method of sending form data to processing page is more used than GET method. In POST method data entered inside the form is passed to action page by using HTTP header. The main difference visible here is that data is not passed through URL as a query string in user address bar (like GET method). Here is the form code for method object.

we can transfer our data like this,

HttpWebRequest request = null;
request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/Default.aspx?");
request.KeepAlive = true;
request.Method = "POST";
string postData = "ID=" + 1 + "&RegKey=" + 2;

byte[] byteArray = Encoding.UTF8.GetBytes(postData);
//request.ContentType = "text/xml";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

Like in the Get method we can also loop through and display all the name value pairs associated in POST method of form submission. Here is the code

Collecting data using Request object

Eg,
string id = Request.Form.Get("ID");
string regKey = Request.Form.Get("RegKey");

Get method and request object to collect data

In GET method data of the form is passed through query string in name and value pairs. The data is separated from the file name by question mark( ? ) and between each pare of name and value ampersand ( & ) symbol is used to separate. Here is the example how in the GET method data is transferred.

http://www.plus2net.com/file_name.asp?name=john&country=USA&age=10


You can see in the above query string the form data is kept after the file name and the question mark ( ? ) . Then name and value pairs are used and they are separated by ampersand ( & ). This is the way data is posted using URL or the address bar of the browser window.

Collecting data using Request object
Once the form is submitted data is available to the page indicated at the action attribute of the form. Here we have to use Request.QueryString to get the value of the data field. To get the name entered by the user in name field here is the code

Eg,
string name = "";
name=Request.QueryString.get("name");

Difference in GET and FORM method in posting data

We can send data to the data processing page by both the GET and POST methods of a form. Both methods are used in form data handling where each one has some difference on the way they work. We will discuss some of the differences.

As you have seen there is a character restriction of 255 in the URL. This is mostly the old browsers restriction and new ones can handle more than that. But we can't be sure that all our visitors are using new browsers. So when we show a text area or a text box asking users to enter some data, then there will be a problem if more data is entered. This restriction is not there in POST method.

In GET method data gets transferred to the processing page in name value pairs through URL, so it is exposed and can be easily traced by visiting history pages of the browser. So any login details with password should never be posted by using GET method.

As the data transfers through address bar ( URL ) there are some restrictions in using space, some characters like ampersand ( & ) etc in the GET method of posting data. We have to take special care for encoding ( while sending ) and decoding ( while receiving ) data if such special characters are present.

There are some special cases where advantage of using GET method is , one can store the name value pairs as bookmark and directly use them by bypassing the form.