Scripting form post in C# & .net
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 = "http://www.google.com";
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);
oWStream.Close();
sResponse = "";
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
Stream oS = oResponse.GetResponseStream();
using (StreamReader oSR = new StreamReader(oS))
{
sResponse = oSR.ReadToEnd();
oSR.Close();
}
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
Related Posts
- Do all versions of OpenCV run at the same speed?
- OpenCV
- Who is WHOIS?
- To RAID or not to RAID
- Script to check available diskspace in C#/.net
- Introduction to OpenCV
- Getting your dynamic public IP in C# & .net
- How to detect the color of a person’s shirt
- Buying vs renting
- Rotating or Resizing an Image in OpenCV