using System; using System.Web; // for HttpUtility class using System.Threading; using System.Text; // for Encoding class using System.Net; // for HttpWebRequest class using System.IO; // for StreamReader class namespace sampleexe { class Class1 { [STAThread] static void Main(string[] args) { string url = "http://localhost:3974/Default.aspx"; string viewstate = InitialViewState(url); viewstate = HttpUtility.UrlEncode(viewstate); string data = "TextBox1=red&TextBox2=empty&Button1=clicked&__VIEWSTATE="+viewstate; byte[] buffer = Encoding.UTF8.GetBytes(data); string proxy = null; WebRequest req = WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = buffer.Length; req.Proxy = new WebProxy(proxy, true); // ignore for local addresses Stream reqst = req.GetRequestStream(); // add form data to request stream reqst.Write(buffer, 0, buffer.Length); reqst.Flush(); reqst.Close(); Console.WriteLine("\nPosting 'red' to " + url); try { req.Credentials = CredentialCache.DefaultCredentials; // HttpWebResponse response = (HttpWebResponse)req.GetResponse(); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); // send request, get response Console.WriteLine("\nResponse stream is: \n"); Stream resst = res.GetResponseStream(); // display HTTP response StreamReader sr = new StreamReader(resst); Console.WriteLine(sr.ReadToEnd()); sr.Close(); resst.Close(); } catch (System.Net.WebException ex) { String responseFromServer = ex.Message.ToString() + " "; if (ex.Response != null) { using (WebResponse response = ex.Response) { Stream data11 = response.GetResponseStream(); using (StreamReader reader = new StreamReader(data11)) { responseFromServer += reader.ReadToEnd(); } } } Console.WriteLine(responseFromServer); } Console.WriteLine("\nDone"); Console.ReadLine(); } // Main() static string InitialViewState(string url) { WebClient wc = new WebClient(); Stream st = wc.OpenRead(url); StreamReader sr = new StreamReader(st); string line; while ((line = sr.ReadLine()) != null) { if (line.IndexOf("__VIEWSTATE") != -1) // found line { sr.Close(); st.Close(); int startIndex = line.IndexOf("value=") + 7; int endIndex = line.IndexOf("\"", startIndex); int count = endIndex - startIndex; return line.Substring(startIndex, count); } } sr.Close(); st.Close(); return "not found"; } // InitialViewState() } // Class1 } // ns PostToASPdotNET
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)