/// <summary> /// 第五步:微信初始化 /// </summary> /// <param name="cookie"></param> /// <returns></returns> public InitSyncResult Init(WechatCookie wechatCookie, string cookieStr) { if (wechatCookie == null) { throw new Exception("cookie值为空"); } string url = string.Format(WechatUrl.InitUrl, wechatCookie.Pass_Ticket, wechatCookie.Skey, WechatCommon.GetTicks()); BaseRequest baseRequest = new BaseRequest(); baseRequest.DeviceID = DeviceID; baseRequest.Sid = wechatCookie.Wxsid; baseRequest.Uin = wechatCookie.Wxuin; baseRequest.Skey = wechatCookie.Skey; InitParamter init = new InitParamter(); init.BaseRequest = baseRequest; string postData = JsonConvert.SerializeObject(init); // string html =await HttpCommon.instance.PostHttp(url, postData, ContentType.json, cookieStr); HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = url, //URL 必需项 Method = "POST", //URL 可选项 默认为Get Encoding = Encoding.UTF8, UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0", //用户的浏览器类型,版本,操作系统 可选项有默认值 Accept = "text/html, application/xhtml+xml, */*", // 可选项有默认值 ContentType = "application/json; charset=UTF-8", //返回类型 可选项有默认值 Cookie = cookieStr, //字符串Cookie 可选项 Postdata = postData, //Post数据 可选项GET时不需要写 }; HttpResult result = http.GetHtml(item); string html = result.Html; AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "Init", html)); InitSyncResult syncKey = new InitSyncResult(); try { if (!string.IsNullOrEmpty(html) && html != "操作超时") { InitResult obj = JsonConvert.DeserializeObject <InitResult>(html); syncKey.SynckeyStr = WechatCommon.SyncKeyFormat(obj.SyncKey); syncKey.SyncKey = obj.SyncKey; syncKey.UserName = obj.User.UserName; syncKey.Uin = obj.User.Uin; syncKey.Nickname = obj.User.NickName; } } catch (Exception ex) { AppLog.WriteError(ex.Message); } return(syncKey); }
private void TimeRun(BackgroundWorker worker) { for (int r = 0; r < Global.robotList.Count; r++) { var robot = Global.robotList[r]; if (robot.State == 0) { string loginUrl = protocol.CheckLogin(robot.UUID); if (loginUrl == "408") { // timerQrCode.Enabled = false; } else if (loginUrl == "200") { // Thread.Sleep(2000); } else { CookieResult cookieResult = protocol.GetCookie(loginUrl); robot.CookieStr = cookieResult.CookieStr; robot.Cookie = cookieResult.wechatCookie; robot.SyncCookie = cookieResult.SyncCookie; InitSyncResult initSyncResult = protocol.Init(robot.Cookie, robot.CookieStr); robot.SyncKey = initSyncResult.SyncKey; robot.SynckeyStr = initSyncResult.SynckeyStr; robot.UserName = initSyncResult.UserName; protocol.StatusNotify(robot.Cookie, robot.CookieStr, robot.UserName); robot.State = 1; Global.robotList[r] = robot; if (worker.CancellationPending) { return; } worker.ReportProgress(1, robot.UUID); } } } }
private void TimeRun(BackgroundWorker worker) { if (worker.CancellationPending) { return; } var robot = Global.robot; if (robot == null) { worker.ReportProgress(1, "noRobot"); } if (robot.State == 0) { worker.ReportProgress(2, "processing"); string loginUrl = protocol.CheckLogin(robot.UUID); worker.ReportProgress(3, "processed"); if (loginUrl == "408") { //超时需要重新生成二维码 } else if (loginUrl == "200") { //已扫描待确认 } else { CookieResult cookieResult = protocol.GetCookie(loginUrl); robot.CookieStr = cookieResult.CookieStr; robot.Cookie = cookieResult.wechatCookie; robot.SyncCookie = cookieResult.SyncCookie; InitSyncResult initSyncResult = protocol.Init(robot.Cookie, robot.CookieStr); robot.Uin = initSyncResult.Uin; RobotService service = new RobotService(); int robotId = service.IsRobot(robot.Uin); if (robotId > 0) { robot.Id = robotId; robot.SyncKey = initSyncResult.SyncKey; robot.SynckeyStr = initSyncResult.SynckeyStr; robot.UserName = initSyncResult.UserName; robot.NickName = initSyncResult.Nickname; protocol.StatusNotify(robot.Cookie, robot.CookieStr, robot.UserName); robot.State = 1; robot.Contact = protocol.GetContact(robot.Cookie, robot.CookieStr).MemberList.ToList(); Global.robot = robot; } else { loginUrl = "noRobot"; } } worker.ReportProgress(1, loginUrl); } else { worker.ReportProgress(1, "1"); } }