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;
}
}
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.