Skip to content

How to log into a website with c#

Oliver Tatlow

Asked Jul 26, 2013, 6:27 AM EDT3 answers
Share
LinkedInX
I'm wanting to return the html from a webpage which requires that I'm logged in.

This is the code I've got so far.

    private
void toolStripButton1_Click(object sender, EventArgs e)  
    {

        var request = WebRequest.Create("http://www.conquerclub.com/game.php?game=13025037");
       
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader
reader = new StreamReader(response.GetResponseStream());
        richTextBox1
.Text = reader.ReadToEnd();
    }

As expected this automatically re-directs to the login page and returns that. How do I pass my username and login so that it will automatically log in and go to the right page?

login name - testuser1
password - testing
(I've created this account so it will actually log in with these credentials)

I've had a look around and found lots of different ways suggested to do this but can't get anything to work. Would be absolutely brilliant if someone could provide code that will make this happen.

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Just I hooked fiddler and your site request. Your site expects lot of cookies stuff.
    During your web request from C#, how you're managing cookies?
    0
  • 0
    Yeah figured that I need to do something with the cookies. Tried this that I found on another site but it's still just bringing back the login page.

    Really struggling to figure out how to do this so any help would be greatly appreciated.

    private void toolStripButton1_Click(object sender, EventArgs e)
            {
                string loginUri = "http://www.conquerclub.com/login.php";;
                string username = "testuser1";
                string password = "testing";
                string reqString = "username=" + username + "&password=" + password;
                byte[] requestData = Encoding.UTF8.GetBytes(reqString);

                CookieContainer cc = new CookieContainer();
                var request = (HttpWebRequest)WebRequest.Create(loginUri);
                request.Proxy = null;
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.Method = "post";

                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = requestData.Length;
                using (Stream s = request.GetRequestStream())
                    s.Write(requestData, 0, requestData.Length);

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.Cookies != null)
                    {
                        foreach (Cookie c in response.Cookies)
                            Console.WriteLine(c.Name + " = " + c.Value);
                    }
                }

                string newloginUri = "http://www.conquerclub.com/game.php?game=13025037";;
                HttpWebRequest newrequest = (HttpWebRequest)WebRequest.Create(newloginUri);
                newrequest.Proxy = null;
                newrequest.CookieContainer = cc;
                using (HttpWebResponse newresponse = (HttpWebResponse)newrequest.GetResponse())
                using (Stream resSteam = newresponse.GetResponseStream())
                using (StreamReader sr = new StreamReader(resSteam))
                    richTextBox1.Text = sr.ReadToEnd();
            }
    0
  • 0
    This works. What I've done here is log in manually then using fiddler copy and pasted the cookies and hardcoded them. This logs straight into the game page as I'm wanting it to. This isn't really a solution though as these cookies are specific to this one user account and I'm guessing they will expire anyway.

    Seems like it's the first part of the code logging in that doesn't work properly. The cookies that come back from this passage of code look completely different to the ones that show up in fiddler so looks like it's just not logging in.



    string newloginUri = "http://www.conquerclub.com/game.php?game=13025037";;
                HttpWebRequest newrequest = (HttpWebRequest)WebRequest.Create(newloginUri);
                newrequest.Proxy = null;
                newrequest.CookieContainer = new CookieContainer();
                newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com";), new Cookie("PHPSESSID","86bte1ipainiq760vm2flv4h13"));
                newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com";), new Cookie("phpbb3_jer7c_u", "648113"));
                newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com";), new Cookie("phpbb3_jer7c_k", ""));
                newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com";), new Cookie("phpbb3_jer7c_sid", "3eebb0771a68c4a58581e495c34b2c93"));
                using (HttpWebResponse newresponse = (HttpWebResponse)newrequest.GetResponse())
                using (Stream resSteam = newresponse.GetResponseStream())
                using (StreamReader sr = new StreamReader(resSteam))
                    richTextBox1.Text = sr.ReadToEnd();
    0