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");

No comments: