Attempting to enter the site by clicking a button

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.

+3


source to share


1 answer


The first thing I noticed about your code is that you are calling twice GetResponse()

on your original login:

HttpWebResponse response = (HttpWebResponse)requestLogin.GetResponse();

      

and

StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());

      



As a result, you enter your login request twice and you will receive two different cookies. This is why the cookie in cookieJar2

does not match what you output later. Reuse your object response

:

StreamReader stIn = new StreamReader(response.GetResponseStream());

      

Then when you try to load the admin page, don't use POST

. The second request should be GET

to get the content of the page.

0


source







All Articles