C# WebClient Log onto Website(C# WebClient 登录网站)
问题描述
我试图通过提供我的(正确)用户名和密码登录网站.
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 登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# WebClient 登录网站
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
