Programmatically Post A Form In ASP.Net

Sometimes, there is a need to post a form from one page to another without any UI element. This is how to do it programmatically:

ASCIIEncoding encoding = new ASCIIEncoding();

byte[] data = encoding.GetBytes(“FormElementName=” + formElementValue);

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(http://somesite/somepage);

myRequest.Method = “POST”;

myRequest.ContentType = “application/x-www-form-urlencoded”;

myRequest.ContentLength = data.Length;

Stream newStream = myRequest.GetRequestStream();

newStream.Write(data, 0, data.Length);

newStream.Close();

Advertisements