Processing webpage post (automated)
-
Hi All! I am trying to process several requests to a website, performing posts of data and processing page results. I'm finding it seems very inefficient, and actually it seems to take longer to process the request than compared to typing in manually. Looking at the code below it appears there isn't a way to simplify or move initialization of objects to another method, rather than performing it each request is made (the requests are being made to the same website). Here is my code below. Thanks for all your help!
private string ProcessFormPOST(string strURL, string strFormData)
{
try
{
//string strViewstate = GetInitialViewState(strURL);
//strViewstate = System.Web.HttpUtility.UrlEncode(strViewstate);
//strFormData += "&__VIEWSTATE=" + strViewstate;byte\[\] buffer = Encoding.UTF8.GetBytes(strFormData); string proxy = null; System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = buffer.Length; req.Proxy = new System.Net.WebProxy(proxy, true); // ignore for local addresses req.CookieContainer = new System.Net.CookieContainer(); // enable cookies System.IO.Stream reqst = req.GetRequestStream(); // add form data to request stream reqst.Write(buffer, 0, buffer.Length); //reqst.Flush(); reqst.Close(); System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse(); // send request, get response System.IO.Stream resst = res.GetResponseStream(); // display HTTP response System.IO.StreamReader sr = new System.IO.StreamReader(resst); return sr.ReadToEnd(); } catch (Exception ex) { throw ex; } }