我正在尝试通过提供我的(正确的)用户名和密码登录网站。
代码如下:
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);
但是,令人惊讶的是,它没有登录。我刚刚恢复了登录页面。
如有任何帮助,我将不胜感激,这让我很头疼!!
谢谢, 吉姆
为完整起见,这里是我的 WebClient 类 -
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;
}
}
最佳答案
如果您尝试在浏览器中登录,这将发布在服务器上:
org.apache.struts.taglib.html.TOKEN=81243ce1a02ff285745f7c25b86234a9&showLogin=true&upgrade=&username=username&password=password&submit=Log+in
也尝试添加这些值,并弄清楚 TOKEN 是如何生成的。
编辑:检查该页面提供给您的 cookie 是否也被提交回来。
另一个编辑:当您发出请求或发布数据时,请查看服务器和浏览器 (=Firefox) 之间发生的情况 LiveHttpHeaders插件。
关于C# WebClient 登录网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1459501/