以下代码能成功登录小米:
[C#] 纯文本查看 复制代码 private bool Login(string loginName, string loginPwd)
{
HttpItem item = new HttpItem() {
URL = "https://account.xiaomi.com/pass/serviceLogin",//URL 必需项
Allowautoredirect = true//是否根据301跳转 可选项
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
string callback = Uri.EscapeDataString(Regex.Match(html, "(?<=callback = encodeURIComponent\\(\").*\"").Value.ToString().Replace("\"", ""));
string sid = Uri.EscapeDataString(Regex.Match(html, "(?<=sid = encodeURIComponent\\(\").*\"").Value.ToString().Replace("\"", ""));
string qs = Uri.EscapeDataString(Regex.Match(html, "(?<=qs = encodeURIComponent\\(\").*\"").Value.ToString().Replace("\"", ""));
string sign = Uri.EscapeDataString(Regex.Match(html, "(?<=sign = encodeURIComponent\\(\").*\"").Value.ToString().Replace("\"", ""));
string hidden = Uri.EscapeDataString(Regex.Match(html, "(?<=hidden = encodeURIComponent\\(\").*\"").Value.ToString().Replace("\"", ""));
var json = true;
var queryPart = string.Format("user={0}&_json={1}&pwd={2}&sid={3}&_sign={4}&callback={5}&qs={6}", loginName, json.ToString().ToLower(), loginPwd,
sid, sign, callback, qs);
item = new HttpItem()
{
URL = "https://account.xiaomi.com/pass/serviceLoginAuth2",//URL 必需项
Method = "post",//URL 可选项 默认为Get
Postdata = queryPart,//Post数据 可选项GET时不需要写
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
Allowautoredirect = true,//是否根据301跳转 可选项
};
result = http.GetHtml(item);
html = result.Html;
cookies = result.Cookie;
var obj = JsonConvert.DeserializeObject<LoginResult>(html.Replace("&&&START&&&", ""));
AppendLog(string.Format("用户名:{0}\t{1}", loginName, obj.desc));
if (!string.IsNullOrEmpty(obj.userId))
{
item = new HttpItem()
{
URL = obj.location,
Allowautoredirect = true,//是否根据301跳转 可选项
Cookie = cookies
};
result = http.GetHtml(item);
cookies += result.Cookie;
return true;
}
return false;
}
效果图:
登录成功图
问题来了,接下来是添加购物车的地址【string.Format("http://order.xiaomi.com/cart/add/{0}-0-1", goodsId)】和下单的地址【http://order.xiaomi.com/buy/checkout】,但传入的cookie跟用抓包工具看到的传入cookie不一致,就引起了下单失败(只能添加进购物车),抓包工具里看到的cookie实在不知道是怎么弄到的,在此求助啊!!! |