Note that there are some explanatory texts on larger screens.

plurals
  1. POperforming http methods using windows application in c#
    text
    copied!<p>There are many sites which call a script upon form submit and pass in parameters using HTTP POST or GET, using a web debugger i have found the parameters being passed. Now i wish to do the same thing through my Windows Application in C#. How can i achieve such a functionality?</p> <p>I am currently using HttpWebRequest and HttpWebResponse class in C#. But it is a pain as i have to write explicit code for each page i try to load and work. For Example i am trying to pass username and password to a php page and taking the response, which will send a cookie and a page in return, based on which i identify if the user has logged in or not.</p> <pre><code>HttpWebRequest loginreq = createreq("http://www.indyarocks.com/mobile/index.php"); String logintext = "username=" + TxtUsrname.Text + "&amp;pass=" + TxtPasswd.Password + "&amp;button.x=0&amp;button.y=0"; loginreq.ContentLength = logintext.Length; StreamWriter writerequest = new StreamWriter(loginreq.GetRequestStream()); writerequest.Write(logintext); writerequest.Close(); HttpWebResponse getloginpageresponse = (HttpWebResponse)loginreq.GetResponse(); cookie = getloginpageresponse.Cookies[0]; BinaryFormatter bf1 = new BinaryFormatter(); Stream f1 = new FileStream("E:\\cookie.dat", FileMode.OpenOrCreate); bf1.Serialize(f1, cookie); f1.Close(); string nexturl = getloginpageresponse.Headers[HttpResponseHeader.Location]; StreamReader readresponse = new StreamReader(getloginpageresponse.GetResponseStream()); if (nexturl == "p_mprofile.php") { MessageBox.Show("Login Successful"); GrpMsg.IsEnabled = true; } else if (nexturl == "index.php?msg=1") { MessageBox.Show("Invalid Credentials Login again"); } </code></pre> <p>This is my createreq class</p> <pre><code> private HttpWebRequest createreq(string url) { HttpWebRequest temp = (HttpWebRequest)WebRequest.Create(url); temp.Method = "POST"; temp.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022; FDM)"; temp.KeepAlive = true; temp.ContentType = "application/x-www-form-urlencoded"; temp.CookieContainer = new CookieContainer(); temp.AllowAutoRedirect = false; return temp; } </code></pre> <p>Am i on the right track? Is there any better way to do it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload