问题描述
我试图通过提供我的(正确)用户名和密码登录网站.
I'm trying to log onto a website by providing my (correct) username and password.
代码如下:
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!!
谢谢,吉姆
为了完整起见,这里是我的 WebClient 类 -
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
也尝试添加这些值,并弄清楚 TOKEN 是如何生成的.
Try adding those values as well, and figure out how TOKEN is generated.
检查该页面给您的 cookie 是否也被提交回来.
Check if cookies that page gives you are submited back too.
另一个当您发出请求或发布数据时,请使用 LiveHttpHeaders 插件.
ANOTHER Too see what is going on between server and browser (=Firefox) when you are making a request or posting data use LiveHttpHeaders addon.
这篇关于C# WebClient 登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!