C# WebClient 登录网站

标签 c# winforms c#-3.0 webforms webclient

我正在尝试通过提供我的(正确的)用户名和密码登录网站。

代码如下:

        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/

相关文章:

c# - 从 PipeReader 读取超时

c# - 如何在 SelectedIndexChanged 事件之前检测 TabControl 中标签页的变化?

c# - 用 3 位数字分隔长号

c# - 在 C# 中预览 PDF

C# 2.0 解析 Excel 电子表格的最快方法

c# - 使用 C# 正则表达式转换字符串中的大小写

c# - WatiN 浏览器中的自定义 BeforeNavigate 事件

c# - 字符串格式参数中的逗号

c# - ASP.NET Core 模型绑定(bind)和整数

c# - winform应用程序的图标绘制工具有哪些?