a01163125 2014-04-10 12:08 采纳率: 0%
浏览 1271

抓包的时候字典里面没有值。

为什么这个会有错误呢? 谁有类似的demo吗?

private void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUserName.Text;
string password = txtPwd.Text;

        HttpItem itemSign = new HttpItem()
        {
            URL = "https://account.xiaomi.com/pass/serviceLogin",
        };
        HttpHelper helperSign = new HttpHelper();
        HttpResult resultSign = helperSign.GetHtml(itemSign);
        string signHtml = resultSign.Html;
        txtInfo.Text = signHtml;
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(signHtml);
        var htmlnodes = doc.DocumentNode.Descendants();
        Dictionary<string, string> dict = new Dictionary<string, string>();
        foreach (var item in htmlnodes)
        {
            if (item.Attributes["name"] != null && item.Attributes["value"] != null)
            {
                dict.Add(item.Attributes["name"].Value, item.Attributes["value"].Value);
            }
        }
        string passToken = HttpUtility.UrlEncode(dict["passToken"]);
        string callback = HttpUtility.UrlEncode(dict["callback"]);
        string sid = HttpUtility.UrlEncode(dict["sid"]);
        string qs = HttpUtility.UrlEncode(dict["qs"]);
        string hidden = HttpUtility.UrlEncode(dict["hidden"]);
        string _sign = HttpUtility.UrlEncode(dict["_sign"]);
        string auto = HttpUtility.UrlEncode(dict["auto"]);

        cookies = resultSign.Cookie;

        HttpItem itemLogin = new HttpItem()         //登陆Post
        {
            URL = "https://account.xiaomi.com/pass/serviceLoginAuth2",
            Method = "POST",
            Cookie = cookies,
            Referer = "https://account.xiaomi.com/pass/serviceLogin",
            ContentType = "application/x-www-form-urlencoded",
            Postdata = string.Format("passToken={0}&user={1}&pwd={2}&callback={3}&sid={4}&qs={5}&hidden={6}&_sign={7}&auto={8}", passToken, username, password, callback, sid, qs, hidden, _sign, auto)
        };
        HttpHelper helperLogin = new HttpHelper();
        HttpResult resultLogin = helperLogin.GetHtml(itemLogin);

        if (resultLogin.Html.Contains("小米帐户 - 登录"))
        {
            AppendText(username + "登陆失败\n"); return;
        }
        AppendText(username + "登陆成功");
        cookies += ";" + resultLogin.Cookie;
        // AppendText(cookies);

    }
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-01 22:41
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在你的C#代码中,你尝试使用Dictionary<string,string>来存储从网页上提取的HTML内容。然而,当你试图将字符串编码为URL时(例如,string passToken = HttpUtility.UrlEncode(dict["passToken"])),它会抛出一个异常。

    这是因为UrlEncode()方法并不是直接用于将字符串转换为URL编码的,而是将其转换为HTTP头部的一部分。如果你想要将字符串转换为真正的URL编码,你需要使用Uri.EscapeDataString()方法。

    以下是修复后的代码:

    private void btnLogin_Click(object sender, EventArgs e)
    
    {
    
        string username = txtUserName.Text;
        string password = txtPwd.Text;
        
        HttpItem itemSign = new HttpItem()
        {
            URL = "https://account.xiaomi.com/pass/serviceLogin"
        };
        HttpHelper helperSign = new HttpHelper();
        HttpResult resultSign = helperSign.GetHtml(itemSign);
        string signHtml = resultSign.Html;
        txtInfo.Text = signHtml;
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(signHtml);
        var htmlnodes = doc.DocumentNode.Descendants();
    
        Dictionary<string, string> dict = new Dictionary<string, string>();
        foreach (var item in htmlnodes)
        {
            if (item.Attributes["name"] != null && item.Attributes["value"] != null)
            {
                dict.Add(item.Attributes["name"].Value, item.Attributes["value"].Value);
            }
        }
    
        string passToken = HttpUtility.UrlDecode(dict["passToken"]);
        string callback = HttpUtility.UrlDecode(dict["callback"]);
        string sid = HttpUtility.UrlDecode(dict["sid"]);
        string qs = HttpUtility.UrlDecode(dict["qs"]);
        string hidden = HttpUtility.UrlDecode(dict["hidden"]);
        string _sign = HttpUtility.UrlDecode(dict["_sign"]);
        string auto = HttpUtility.UrlDecode(dict["auto"]);
    
        cookies = resultSign.Cookie;
    
        HttpItem itemLogin = new HttpItem()         //登陆Post
        {
            URL = "https://account.xiaomi.com/pass/serviceLoginAuth2",
            Method = "POST",
            Cookie = cookies,
            Referer = "https://account.xiaomi.com/pass/serviceLogin",
            ContentType = "application/x-www-form-urlencoded",
            Postdata = string.Format("passToken={0}&user={1}&pwd={2}&callback={3}&sid={4}&qs={5}&hidden={6}&_sign={7}&auto={8}", passToken, username, password, callback, sid, qs, hidden, _sign, auto)
        };
    
        HttpHelper helperLogin = new HttpHelper();
        HttpResult resultLogin = helperLogin.GetHtml(itemLogin);
    
        if (resultLogin.Html.Contains("小米帐户 - 登录"))
        {
            AppendText(username + "登陆失败\n"); return;
        }
        AppendText(username + "登陆成功");
        cookies += ";" + resultLogin.Cookie;
    }
    

    在这个版本的代码中,我将UrlEncode()替换为了UrlDecode(),并使用了Uri.EscapeDataString()来处理URL编码。这应该能解决你在捕获到的错误。

    评论

报告相同问题?