Trying to login to a website clicking a button

Go To


I've just started playing around with C# few weeks ago. i got a task i am trying to perform and not sure if the way i do it now is the right approach.

I am trying to login to a website(in my case WordPress website- for lake of better options) and navigating in admin panel using C#

So far what I've done was creating a new project - Windows Form Application.

The 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= In";
    string strResponse;
    HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("");
    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);

I then have this code to to take the cookie of the response.

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 response + close some streams.

StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
string responseFromServer = stIn.ReadToEnd();
webBrowser1.DocumentText = responseFromServer;

And then i try using the above cookie for the page i am trying to access as follows :

HttpWebRequest requestLogin2 = (HttpWebRequest)WebRequest.Create("");
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);

StreamReader stIn2 = new StreamReader(requestLogin2.GetResponse().GetResponseStream());
strResponse = stIn2.ReadToEnd();
string responseFromServer2 = stIn2.ReadToEnd();
webBrowser1.DocumentText = responseFromServer2;
richTextBox2.Text += "\n\n\n" + responseFromServer2;

Well it doesn't work for some reason I've been trying this for a week now.

I tried displaying the header - after the first request to see what headers i get back. and then looked at the cookie i built (cookieJar2) and it seem they aren't the same..

Anyways any help on the matter would be awesome and highly appreciated. i tried to give as much details as i could.

2012-04-04 07:02
by Yaniv Kossas
No matter what i do i keep only getting the response cookie and i think because of that i am missing the other 4 cookies.. don't know what to do with this - Yaniv Kossas 2012-04-04 10:46
Anyone with any idea - Yaniv Kossas 2012-04-04 22:18
Still Looking for any regarding the matte - Yaniv Kossas 2012-04-05 12:14
No one ? not even direction - Yaniv Kossas 2012-04-06 04:06


The first thing I notice about your code is that you call GetResponse() twice in your initial login:

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


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

As a result, you're making the login request twice and will be getting back two different cookies. This will be why the cookie in cookieJar2 doesn't match what you're outputting later. Reuse your response object:

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

Next, when you try to load the admin page, don't POST to it. Your second request should be a GET to retrieve the content of the page.

2012-04-11 21:36
by EAMann