小米抢购(简单版)-登录并验证抢购权限,以及获取真实抢购地址!

class="brush:javascript;gutter:true;">hdcontrol({ "stime": 100066662069666032, "status": { "allow": true, "miphone": { "hdstart": true, "hdstop": false, "hdurl": "?_a=20131022_phone_zxxxxa0c0e3e&_op=choose", "duration": null, "pmstart": false }, "mibox": { "hdstart": true, "hdstop": false, "hdurl": "?_a=20131022_box_aeb5xxxxxxb&_op=choose", "duration": null, "pmstart": false } } })
说明:(抢购前1-2个钟才开放此链接,正式开始时才返回真实地址)
allow 为true 标识为 有权限购买 false 标识为 没有权限购买
可用正则 也可以用json
"(allow|hdstart|hdurl)":("(.+?)"|(.+?))(,)
"allow":(?<allow>.+?,)
"hdstart":(?<hdstart>.+?,)
"hdurl":(?<hdurl>"(.+?)",)
json实体类:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using xiaomi.xiaomiEntityJsonTypes;
namespace xiaomi.xiaomiEntityJsonTypes
{
public class Miphone
{
[JsonProperty("hdstart")]
public bool Hdstart;
[JsonProperty("hdstop")]
public bool Hdstop;
[JsonProperty("hdurl")]
public string Hdurl;
[JsonProperty("duration")]
public object Duration;
[JsonProperty("pmstart")]
public bool Pmstart;
}
public class Mibox
{
[JsonProperty("hdstart")]
public bool Hdstart;
[JsonProperty("hdstop")]
public bool Hdstop;
[JsonProperty("hdurl")]
public string Hdurl;
[JsonProperty("duration")]
public object Duration;
[JsonProperty("pmstart")]
public bool Pmstart;
}
public class Mitv
{
[JsonProperty("hdstart")]
public bool Hdstart;
[JsonProperty("hdstop")]
public bool Hdstop;
[JsonProperty("hdurl")]
public string Hdurl;
[JsonProperty("duration")]
public object Duration;
[JsonProperty("pmstart")]
public bool Pmstart;
}
public class Status
{
[JsonProperty("allow")]
public bool Allow;
[JsonProperty("miphone")]
public Miphone Miphone;
[JsonProperty("mibox")]
public Mibox Mibox;
[JsonProperty("mitv")]
public Mitv Mitv;
}
}
namespace xiaomi
{
public class xiaomiEntity
{
[JsonProperty("stime")]
public int Stime;
[JsonProperty("status")]
public Status Status;
}
}
实现:
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Configuration;
namespace xiaomi
{
public partial class Form1 : Form
{
private static System.Timers.Timer _queuetimer;
private static string cookies = string.Empty; //公有Cookie
private static string codeCookie = string.Empty;
public Form1()
{
InitializeComponent();
txtUserName.Text = ConfigurationManager.AppSettings["userName"].ToString();
txtPwd.Text = ConfigurationManager.AppSettings["userPwd"].ToString();
}
private void btnStart_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
int t = Convert.ToInt32(txtMilliseconds.Text);
TimerStart(t);
}
else
{
QiangGou();
}
btnStart.Enabled = false;
btnStop.Enabled = true;
}
private void btnStop_Click(object sender, EventArgs e)
{
TimerStop();
btnStart.Enabled = true;
btnStop.Enabled = false;
}
public void TimerStart(int Interval)
{
if (_queuetimer == null)
{
_queuetimer = new System.Timers.Timer();
}
else
{
_queuetimer.Close(); _queuetimer = new System.Timers.Timer();
}
_queuetimer.Interval = Interval;
_queuetimer.Elapsed += (sender, e) => _queuetimer_Elapsed(sender, e);
_queuetimer.AutoReset = true;
_queuetimer.Enabled = true;
}
public void TimerStop()
{
if (_queuetimer != null)
{
_queuetimer.Enabled = false;
_queuetimer.Stop();
_queuetimer.Close();
}
}
void _queuetimer_Elapsed(object sender, ElapsedEventArgs e)
{
Parallel.Invoke(CreateTaskArray(10, QiangGou));
}
/// <summary>
/// 创建多个任务
/// </summary>
/// <param name="taskCount"></param>
/// <returns></returns>
private static Action[] CreateTaskArray(int taskCount, Action Dequeue)
{
var actions = new Action[taskCount];
for (int i = 0; i < taskCount; i++)
{
actions[i] = Dequeue;
}
return actions;
}
public void QiangGou()
{
//AppendText(cookies);//打印
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "http://tc.hd.xiaomi.com/hdget?callback=hdcontrol",
UserAgent = "Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; MI-ONE C1 Build/IMM76D) UC AppleWebKit/534.31 (KHTML, like Gecko) Mobile Safari/534.31",
Host = "tc.hd.xiaomi.com",
Cookie = cookies
};
HttpResult result = http.GetHtml(item);
string strJson = result.Html;
if (strJson.Contains("hdcontrol"))
{
strJson = strJson.Replace("hdcontrol(", "").Replace(")", "");
xiaomiEntity xm = JsonConvert.DeserializeObject<xiaomiEntity>(strJson);
bool allow = xm.Status.Allow;
string Hdurl = xm.Status.Miphone.Hdurl;
if (!string.IsNullOrEmpty(Hdurl))
{
string url = "http://t.hd.xiaomi.com/s/" + xm.Status.Miphone.Hdurl + "&_m=1";
if (allow)
{
lblAllow.Invoke(new Action(delegate() { lblAllow.Text = allow.ToString(); }));
txtUrl.Invoke(new Action(delegate() { txtUrl.Text = url; }));
TimerStop();
}
else
{
bool allowchecked = false;
ckbAllow.Invoke(new Action(delegate() { allowchecked = ckbAllow.Checked; }));
if (allowchecked == true)
{
txtUrl.Invoke(new Action(delegate() { txtUrl.Text = url; }));
}
}
}
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUserName.Text;
string password = txtPwd.Text;
HttpItem itemSign = new HttpItem() //获取_sign
{
URL = "https://account.xiaomi.com/pass/serviceLogin",
};
HttpHelper helperSign = new HttpHelper();
HttpResult resultSign = helperSign.GetHtml(itemSign);
// <input type="hidden" name="_sign" value="KKkRzxzZoDC+gLdeyOszxzV0Xg=">
string _sign = System.Uri.EscapeDataString(Regex.Match(resultSign.Html, "name=\"_sign\" value=\"(.+?)\"").Groups[1].Value);
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 = "passToken=&user=" + username + "&pwd=" + password + "&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&qs=%253Fsid%253Dpassport&hidden=&_sign=KKkRvCpZoDC%2BgLdeyOsdMhwV0Xg%3D&auto=true"
};
HttpHelper helperLogin = new HttpHelper();
HttpResult resultLogin = helperLogin.GetHtml(itemLogin);
if (resultLogin.Html.Contains("小米帐户 - 登录"))
{
AppendText(username + "登陆失败\n");
return;
}
AppendText(username + "登陆成功");
cookies += ";" + resultLogin.Cookie;
// AppendText(cookies);
}
private void btnCopy_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtUrl.Text))
{
Clipboard.SetDataObject(txtUrl.Text, true);
}
}
protected void AppendText(string info)
{
txtInfo.Invoke((MethodInvoker)delegate
{
txtInfo.AppendText(info + Environment.NewLine);
txtInfo.SelectionStart = txtInfo.Text.Length;
txtInfo.ScrollToCaret();
});
}
}
}
baidu-yun:小米抢购v0.1-rar 下载