Scripting form post in C# & .net

Earlier I blogged about scripting web browsing in .NET and C#. While automating fetching of simple webpages using GET method is pretty simple, form posting and retrieving response can be even more challenging.

I had a tough time getting form posting to work. But once you have the right script, it is pretty simple to redo it or customize for another site.

Here is the sample script:

string sURL = "";

string sPostData = "field1=value1&field2=value2";
string sResponse = "";

HttpWebRequest oRequest = (HttpWebRequest)System.Net.HttpWebRequest.Create(sURL);
oRequest.Method = "POST";
oRequest.ContentType = "application/x-www-form-urlencoded";
oRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
oRequest.ContentLength = sPostData.Length;

UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(sPostData);

Stream oWStream = oRequest.GetRequestStream();
oWStream.Write(bytes, 0, bytes.Length);

sResponse = "";

HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
Stream oS = oResponse.GetResponseStream();
using (StreamReader oSR = new StreamReader(oS))
sResponse = oSR.ReadToEnd();


The five key things to note are:
1) Setting method to POST
2) Setting ContentType to application/x-www-form-urlencoded
3) Setting ContentLength to the length of posted data
4) Formatting the posted data correctly
5) Encoding the posted data and sending it as a stream

Formatting the posted data correctly is very important, otherwise the server will not recognize the data and will return unwanted results.

Form field name and value should be represented as name=value and multiple name/value pairs separated by & sign like this: field1=value1&field2=value2

Leave a Reply

Your email address will not be published. Required fields are marked *