views:

1018

answers:

4

Hello, I'm trying to log onto a website by providing my (correct) username and password.

Here's the code:

        string URL = @"https://www.t-mobile.co.uk/service/your-account/login/";

        string username = "a_user";
        string password = "a_password";
        //ServicePointManager.Expect100Continue = false;

        CookieAwareClient client = new CookieAwareClient();


        NameValueCollection postData = new NameValueCollection();
        postData.Add("username", username);
        postData.Add("password", password);

        byte[] response = client.UploadValues(URL,  postData);

        ASCIIEncoding enc = new ASCIIEncoding();
        string Source = enc.GetString(response);

But, surprise surprise, it's not logging on. I just get the logon page back.

Any help would be appreciated and this is doing my head in now!!

Thanks, Jim

For completeness here is my WebClient class -

public class CookieAwareClient : WebClient
{
    private CookieContainer m_container = new CookieContainer();
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = m_container;
        }
        return request;
    }
}
A: 

You could try adding a user agent, like:

client.Headers.Add("user-agent", "your user agent here");

Sometimes web sites want to assure that a certain browser is used and/or pretends to be human.

Jonatan Lindén
Hello Jonatan,As per your recommendation I added the following line:client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");Unfortunately, this did not help.Thanks,Jim
Jim
A: 

I see 1 initial problem, you are posting back to the page url and not the submit URL (which has a session ID on it, and in my code I'd be checking for a valid session ID). There is also a javascript function called prior to the submit 'processSaveUserCh()' which is creating a cookie for the username. The omission of any of this may be preventing your successful login, it's an experiment and discover exercise I'm afraid.

Lazarus
A: 

Hi

This is posted on server if you try to login in browser:

org.apache.struts.taglib.html.TOKEN=81243ce1a02ff285745f7c25b86234a9&showLogin=true&upgrade=&username=username&password=password&submit=Log+in

Try adding those values as well, and figure out how TOKEN is generated.

EDIT: Check if cookies that page gives you are submited back too.

ANOTHER EDIT: Too see what is going on between server and browser (=Firefox) when you are making a request or posting data use LiveHttpHeaders addon.

grega g
Grega,Thank you - That's solved - although I haven't had chance to work out what TOKEN is yet - I'll do that now.Thanks for your help!One thing, how did you work out what else was getting posed?Thanks again,Jim
Jim
A: 

Also I notice that you are creating a new CookieContainer each time you create your CookieAwareClientn class. This means that your cookies are not being saved between requests. Make that member static, so that you use the same cookiecontainer always.

Also like other said, you will have to experiment. WHat I do is, I download and install Firebug, and see how the request/response is beingn sent by the browser, along with the headers, etc. I then duplicate the same request using HttpWebRequest/WebClient.

feroze