/// <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); }
/// <summary> /// /// </summary> /// <returns></returns> public string StatusNotify(WechatCookie wechatCookie, string cookieStr, string username) { string url = string.Format(WechatUrl.NotifyUrl, wechatCookie.Pass_Ticket); StatusNotifyParamter param = new StatusNotifyParamter(); param.Code = 3; param.ClientMsgId = WechatCommon.GetTicks(); param.FromUserName = username; param.ToUserName = username; BaseRequest request = new BaseRequest(); request.DeviceID = DeviceID; request.Sid = wechatCookie.Wxsid; request.Skey = wechatCookie.Skey; request.Uin = wechatCookie.Wxuin; param.BaseRequest = request; string postData = JsonConvert.SerializeObject(param); //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; string html = string.Empty; html = HttpCommon.instance.PostHttp(url, postData, ContentType.json, cookieStr); AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "StatusNotify", html)); return(html); }
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); } }
/// <summary> /// 第四步:登录获取cookie /// </summary> /// <param name="url"></param> /// <returns></returns> public CookieResult GetCookie(string url) { if (url.StartsWith("?ticket")) { url = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage" + url; } HttpHelper http = new HttpHelper(); 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); CookieResult cookieResult = new CookieResult(); WechatCookie cookie = new WechatCookie(); string html = result.Html; AppLog.WriteInfo(string.Format("协议:{0},结果:{1}", "GetCookie", html)); if (!string.IsNullOrEmpty(html) && html != "操作超时") { XmlDocument doc = new XmlDocument(); doc.LoadXml(html); string json = JsonConvert.SerializeXmlNode(doc); ///改下代码,定义类 dynamic d = JsonConvert.DeserializeObject <dynamic>(json); cookie.Skey = d.error.skey.Value; cookie.Pass_Ticket = d.error.pass_ticket.Value; cookie.Wxsid = d.error.wxsid.Value; cookie.Wxuin = Convert.ToInt32(d.error.wxuin.Value); } cookieResult.wechatCookie = cookie; #region 核心cookie处理 string cookieStr = result.Cookie; cookieStr = cookieStr.Replace("Domain=wx.qq.com; Path=/;", "").Replace("Domain=.qq.com; Path=/;", "").Replace(" ", ""); StringBuilder sb = new StringBuilder(); string[] c = cookieStr.Split(';'); sb.Append(c[0] + ";"); for (int i = 1; i < 7; i++) { int firstIndex = c[i].LastIndexOf(','); string t = c[i].Substring(firstIndex + 1) + ";"; sb.Append(t); } if (cookieStr.IndexOf("webwx_data_ticket=") >= 0) { string tmp = cookieStr.Substring(cookieStr.IndexOf("webwx_data_ticket=")); int endIndex = tmp.IndexOf(";"); cookieResult.SyncCookie = tmp.Substring(0, endIndex); } cookieResult.CookieStr = sb.ToString(); #endregion return(cookieResult); }