Trying to enter the site by clicking a button (Httpwebrequest - cookies)
I just started playing with C # a few weeks ago. I have a task that I am trying to accomplish and I don't know if this is right for me now.
I am trying to login to a website (in my case a WordPress site - for the best lake option) and go to the admin panel using C #
So far I have created a new project, a Windows Form Application.
Following code - send a request to the website with password / username and other parameters as POST
private void button2_Click(object sender, EventArgs e)
{
CookieContainer cookieJar = new CookieContainer();
CookieContainer cookieJar2 = new CookieContainer(); // will use this later
string testaa = ""; // will use this later
string paramaters = "log=xxxx&pwd=xxxx&testcookie=1&redirect_to=http://www.example.com/wp-admin/&wp-submit=Log In";
string strResponse;
HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("http://www.lyndatobin-howes.com/wp-login.php");
requestLogin.Method = "POST";
requestLogin.AllowAutoRedirect = false;
requestLogin.ContentType = "application/x-www-form-urlencoded";
requestLogin.CookieContainer = cookieJar;
requestLogin.ContentLength = paramaters.Length;
StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), Encoding.ASCII);
stOut.Write(paramaters);
stOut.Close();
}
Then I get this code to grab the response cookie.
HttpWebResponse response = (HttpWebResponse)requestLogin.GetResponse();
foreach (Cookie c in response.Cookies)
{
cookieJar2.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
}
then I have this to read the answer + close some threads.
StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
string responseFromServer = stIn.ReadToEnd();
webBrowser1.DocumentText = responseFromServer;
stIn.Close();
response.Close();
And then I am trying to use the above cookie for the page I am trying to access like this:
HttpWebRequest requestLogin2 = (HttpWebRequest)WebRequest.Create("http://www.example.com/wp-admin/");
requestLogin2.Method = "POST";
requestLogin2.AllowAutoRedirect = false;
requestLogin2.ContentType = "application/x-www-form-urlencoded";
requestLogin2.CookieContainer = cookieJar2;
requestLogin2.ContentLength = paramaters.Length;
StreamWriter stOut2 = new StreamWriter(requestLogin2.GetRequestStream(), System.Text.Encoding.ASCII);
stOut2.Write(paramaters);
stOut2.Close();
StreamReader stIn2 = new StreamReader(requestLogin2.GetResponse().GetResponseStream());
strResponse = stIn2.ReadToEnd();
string responseFromServer2 = stIn2.ReadToEnd();
webBrowser1.DocumentText = responseFromServer2;
richTextBox2.Text += "\n\n\n" + responseFromServer2;
stIn.Close();
Well it doesn't work for some reason, I've been trying this for a week now.
I tried to display the header - after the first request, see what headers I will return. and then looked at the cookies I built (cookieJar2) and it seems they are not the same.
Any help on this issue would be awesome and much appreciated anyway. I have tried to give as much detail as possible.
source to share
Use Fiddler and / or Wireshark and compare successful requests (made with some browser) and failed requests ... then simulate successful requests in your code by removing / adding everything you need, very often Cookies are one of the "culprits" in such situations - sometimes is it a specific referent or similar ...
Some sites are again using automatic login safeguards ...
Besides the coding part: check if automatic registration is thus prohibited by their terms / conditions!
source to share