static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); AppLog.WriteError(str); }
/// <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); }
/// <summary> /// 第六步:获取消息内容 /// </summary> /// <returns></returns> public SyncResult Sync(WechatCookie wechatCookie, string cookieStr, SyncKey syncKey) { if (wechatCookie == null) { throw new Exception("cookie值为空"); } string url = string.Format(WechatUrl.SnycUrl, wechatCookie.Wxsid, wechatCookie.Skey, wechatCookie.Pass_Ticket); SyncParamter param = new SyncParamter(); param.SyncKey = syncKey; param.rr = WechatCommon.GetTicks(); BaseRequest request = new BaseRequest(); request.Uin = wechatCookie.Wxuin; request.DeviceID = DeviceID; request.Sid = wechatCookie.Wxsid; request.Skey = wechatCookie.Skey; param.BaseRequest = request; string postData = JsonConvert.SerializeObject(param); string html = HttpCommon.instance.PostHttp(url, postData, ContentType.json, cookieStr); AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "Sync", html)); //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; SyncResult sync = new SyncResult(); try { if (!string.IsNullOrEmpty(html) && html != "操作超时") { sync = JsonConvert.DeserializeObject <SyncResult>(html); //SyncKey = sync.SyncKey; //SyncKeyFormat(); //msg = sync.AddMsgList; } } catch (Exception ex) { AppLog.WriteError(ex.Message); } return(sync); }
/// <summary> /// 验证用户信息(加好友) /// </summary> public BaseResponse VerifyUser(string userName, string ticket, WechatCookie wechatCookie, string cookieStr) { string url = string.Format(WechatUrl.VerifyUserUrl, WechatCommon.GetTicks(), wechatCookie.Pass_Ticket); VerifyUserParamter param = new VerifyUserParamter(); BaseRequest request = new BaseRequest(); request.DeviceID = DeviceID; request.Sid = wechatCookie.Wxsid; request.Skey = wechatCookie.Skey; request.Uin = wechatCookie.Wxuin; param.BaseRequest = request; param.Opcode = 3; param.SceneList = new int[] { 3 }; param.SceneListCount = 1; param.skey = wechatCookie.Skey; param.VerifyContent = ""; param.VerifyUserListSize = 1; IList <VerifyUserList> verifyUserList = new List <VerifyUserList>(); VerifyUserList verifyUser = new VerifyUserList(); verifyUser.Value = userName; verifyUser.VerifyUserTicket = ticket; verifyUserList.Add(verifyUser); param.VerifyUserList = verifyUserList; string postData = JsonConvert.SerializeObject(param); string html = HttpCommon.instance.PostHttp(url, postData, ContentType.json, cookieStr); AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "VerifyUser", html)); BaseResponse response = new BaseResponse(); try { if (!string.IsNullOrEmpty(html) && html != "操作超时") { response = JsonConvert.DeserializeObject <BaseResponse>(html); } } catch (Exception ex) { AppLog.WriteError(ex.Message); } return(response); }
/// <summary> /// 获取消息前同步(轮询同步有消息后执行Sync) /// </summary> /// <param name="host"></param> public SyncCheckResult SyncCheck(string host, WechatCookie wechatCookie, string cookieStr, string SynckeyStr) { string url = string.Format(WechatUrl.SnycCheckUrl, WechatCommon.GetTicks(), HttpHelper.URLEncode(wechatCookie.Skey), wechatCookie.Wxsid, wechatCookie.Wxuin, DeviceID, SynckeyStr, WechatCommon.GetTicks(), host); string html = HttpCommon.instance.Login(url, cookieStr); AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "SyncCheck", html)); //HttpItem item = new HttpItem() //{ // URL = url,//URL 必需项 // Method = "GET",//URL 可选项 默认为Get // UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36",//用户的浏览器类型,版本,操作系统 可选项有默认值 // Accept = "application/json, text/plain, */*",// 可选项有默认值 // ContentType = "application/json; charset=UTF-8",//返回类型 可选项有默认值 // // Encoding = Encoding.UTF8, // Cookie = syncCookie,//字符串Cookie 可选项 // Referer = "https://wx.qq.com/?&lang=zh_CN", //}; //HttpResult result = http.GetHtml(item); //string html = result.Html; SyncCheckResult syncCheck = new SyncCheckResult(); try { if (!string.IsNullOrEmpty(html) && html != "请求超时") { if (html.IndexOf('{') != -1) { string sycheck = html.Substring(html.IndexOf('{')); syncCheck = JsonConvert.DeserializeObject <SyncCheckResult>(sycheck); } } } catch (Exception ex) { AppLog.WriteError(ex.Message); } return(syncCheck); }
public BatchGetContactResult BatchGetContact(List <string> userNameList, WechatCookie wechatCookie, string cookieStr) { string url = string.Format(WechatUrl.BatchContentUrl, WechatCommon.GetTicks(), wechatCookie.Pass_Ticket); BatchGetContactParamter batch = new BatchGetContactParamter(); BaseRequest request = new BaseRequest(); request.DeviceID = DeviceID; request.Uin = wechatCookie.Wxuin; request.Sid = wechatCookie.Wxsid; request.Skey = wechatCookie.Skey; batch.BaseRequest = request; List <ChatRoomIdList> list_chatRoomId = new List <ChatRoomIdList>(); foreach (var username in userNameList) { ChatRoomIdList chatRoomId = new ChatRoomIdList(); chatRoomId.EncryChatRoomId = ""; chatRoomId.UserName = username; list_chatRoomId.Add(chatRoomId); } batch.List = list_chatRoomId; batch.Count = list_chatRoomId.Count; string postData = JsonConvert.SerializeObject(batch); string html = HttpCommon.instance.PostHttp(url, postData, ContentType.json, cookieStr); AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "BatchGetContact", html)); BatchGetContactResult result = new BatchGetContactResult(); try { if (!string.IsNullOrEmpty(html) && html != "操作超时") { result = JsonConvert.DeserializeObject <BatchGetContactResult>(html); } } catch (Exception ex) { AppLog.WriteError(ex.Message); } return(result); }
/// <summary> /// 第三步:轮询检测是否扫描登录 /// </summary> /// <param name="uuid"></param> /// <returns></returns> public string CheckLogin(string uuid) { string LoginUrl = ""; string url = string.Format(WechatUrl.CheckLoginUrl, uuid, WechatCommon.GetTicks()); string html = HttpCommon.instance.GetHttp(url, ContentType.html); AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "CheckLogin", html)); //408 登陆超时 //201 扫描成功 //200 确认登录 try { if (!string.IsNullOrEmpty(html) && html != "操作超时") { if (html.IndexOf("408") > 0) { LoginUrl = "408"; } else if (html.IndexOf("200") == -1) { LoginUrl = "200"; } else { Regex reg = new Regex("\"(.*?)\""); Match m = reg.Match(html); if (m.Success) { LoginUrl = m.Value; LoginUrl = LoginUrl.Substring(1, LoginUrl.Length - 2); } } } } catch (Exception ex) { AppLog.WriteError(ex.Message); } return(LoginUrl); }
/// <summary> /// 第一步:获取UUID /// </summary> /// <returns></returns> public string GetUUid() { string url = string.Format(WechatUrl.UUIDUrl, WechatCommon.GetTicks()); string uuid = string.Empty; string html = HttpCommon.instance.GetHttp(url, ContentType.html); AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "GetUUid", html)); //HttpItem item = new HttpItem() //{ // URL = url,//URL 必需项 // Method = "GET",//URL 可选项 默认为Get // UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",//用户的浏览器类型,版本,操作系统 可选项有默认值 // Accept = "text/html, application/xhtml+xml, */*",// 可选项有默认值 // ContentType = "text/html",//返回类型 可选项有默认值 //}; //HttpResult result = http.GetHtml(item); //string html = result.Html; try { if (!string.IsNullOrEmpty(html) && html != "操作超时") { if (html.IndexOf("200") != -1) { Regex reg = new Regex("\"(.*?)\""); Match m = reg.Match(html); if (m.Success) { uuid = m.Value; uuid = uuid.Substring(1, uuid.Length - 2); } } } } catch (Exception ex) { AppLog.WriteError(ex.Message); } return(uuid); }
/// <summary> /// 获取联系人信息 /// </summary> /// <returns></returns> public ContactResult GetContact(WechatCookie wechatCookie, string cookieStr) { string url = string.Format(WechatUrl.ContentUrl, wechatCookie.Pass_Ticket, WechatCommon.GetTicks(), wechatCookie.Skey); string html = HttpCommon.instance.PostHttp(url, "{}", ContentType.json, cookieStr); AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "GetContact", html)); ContactResult contact = new ContactResult(); try { if (!string.IsNullOrEmpty(html) && html != "操作超时") { contact = JsonConvert.DeserializeObject <ContactResult>(html); } } catch (Exception ex) { AppLog.WriteError(ex.Message); } return(contact); }
static void Main() { try { //设置应用程序处理异常方式:ThreadException处理 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); #region 应用程序的主入口点 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormMain()); #endregion } catch (Exception ex) { string str = GetExceptionMsg(ex, string.Empty); MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); AppLog.WriteError(str); } }