C# WebClient 登录网站

C# WebClient Log onto Website(C# WebClient 登录网站)
本文介绍了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 登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)