C#WebClient登录网站 [英] C# WebClient Log onto Website

查看:217
本文介绍了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 =用户名& password =密码& submit =登录

也尝试添加这些值,并弄清楚如何生成令牌。

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.

另一项编辑:太清楚了进行请求或发布数据时在服务器和浏览器(= Firefox)之间进行操作,请使用 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登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆