/// <summary> /// 接收聊天图片base64 /// </summary> /// <param name="MsgId"></param> /// <returns></returns> public static string Image(string MsgId) { Cookie sid = RequstService.GetCookie("wxsid"); Cookie uin = RequstService.GetCookie("wxuin"); if (sid != null && uin != null) { byte[] bytes = RequstService.SendGetRequest(InterFaceURL._image_url + sid.Value + "&lang=zh_CN&skey=" + LoginService.sKey + "&pass_ticket=" + LoginService.pass_Ticket + "&username="******"&type=slave&MsgID=" + MsgId); return(Convert.ToBase64String(bytes)); } else { return(null); } }
/// <summary> /// 发送消息 /// </summary> /// <param name="msg"></param> /// <param name="from"></param> /// <param name="to"></param> /// <param name="type"></param> public void SendMsg(string msg, string from, string to, int type) { string msg_json = ""; msg_json = "{{" + "\"BaseRequest\":{{" + "\"DeviceID\" : \"e441551176\"," + "\"Sid\" : \"{0}\"," + "\"Skey\" : \"{6}\"," + "\"Uin\" : \"{1}\"" + "}}," + "\"Msg\" : {{" + "\"ClientMsgId\" : {8}," + "\"Content\" : \"{2}\"," + "\"FromUserName\" : \"{3}\"," + "\"LocalID\" : {9}," + "\"ToUserName\" : \"{4}\"," + "\"Type\" : {5}" + "}}," + "\"rr\" : {7}" + "}}"; Cookie sid = RequstService.GetCookie("wxsid"); Cookie uin = RequstService.GetCookie("wxuin"); if (sid != null && uin != null) { msg_json = string.Format(msg_json, sid.Value, uin.Value, msg, from, to, type, LoginService.sKey, DateTime.Now.Millisecond, DateTime.Now.Millisecond, DateTime.Now.Millisecond); byte[] bytes = null; switch (type) { case 1: bytes = RequstService.SendPostRequest(InterFaceURL._sendmsg_url + sid.Value + "&lang=zh_CN&pass_ticket=" + LoginService.pass_Ticket, msg_json); break; case 3: bytes = RequstService.SendPostRequest(InterFaceURL._send_image_url + sid.Value + "&lang=zh_CN&pass_ticket=" + LoginService.pass_Ticket, msg_json); break; } string send_result = Encoding.UTF8.GetString(bytes); } }
/// <summary> /// 获取二维码 /// </summary> /// <returns></returns> public Image GetQRCode() { try { string datetime = GetTimeStamp(); //获取UUID的URL string _appid_url = "https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&fun=new&lang=zh_CN&_=" + datetime + ""; //获取二维码的URL string _qrcode_url = "https://login.weixin.qq.com/qrcode/"; //后面增加UUID byte[] bytes = RequstService.SendGetRequest(_appid_url); _uuid = Encoding.UTF8.GetString(bytes).Split(new string[] { "\"" }, StringSplitOptions.None)[1]; bytes = RequstService.SendPostRequest(_qrcode_url + _uuid, "t=webwx&_=" + datetime + ""); Image qrCode = Image.FromStream(new MemoryStream(bytes)); return(qrCode); } catch (Exception ex) { throw; } }
/// <summary> /// 登录扫描检测 /// </summary> /// <returns></returns> public object LoginScanDetection() { try { if (_uuid == null) { return(null); } string datetime = GetTimeStamp(); //等待登录 判断二维码扫描情况 408 登陆超时 201 扫描成功 200 确认登录 string _loginScanDetection_url = "https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&_=" + datetime + "&tip=" + tip + "&uuid="; //后面增加UUID byte[] bytes = RequstService.SendGetRequest(_loginScanDetection_url + _uuid); if (bytes == null) { return(null); } string loginResult = Encoding.UTF8.GetString(bytes); if (loginResult.Contains("=201")) // 已扫描二维码但未登录 { string base64_image = loginResult.Split(new string[] { "\'" }, StringSplitOptions.None)[1].Split(',')[1]; byte[] base64_image_bytes = Convert.FromBase64String(base64_image); MemoryStream memoryStream = new MemoryStream(base64_image_bytes, 0, base64_image_bytes.Length); memoryStream.Write(base64_image_bytes, 0, base64_image_bytes.Length); return(Image.FromStream(memoryStream)); } else if (loginResult.Contains("=200")) //已扫描二维码并完成登录 { string login_redirect_url = loginResult.Split(new string[] { "\"" }, StringSplitOptions.None)[1]; return(login_redirect_url); } else { return(null); } } catch (Exception ex) { throw; } }
/// <summary> /// 微信同步(获取最新消息) /// </summary> /// <returns></returns> public static JObject WxSync() { string sync_json = "{{\"BaseRequest\" : {{\"DeviceID\":\"e1615250492\",\"Sid\":\"{1}\", \"Skey\":\"{5}\", \"Uin\":\"{0}\"}},\"SyncKey\" : {{\"Count\":{2},\"List\":[{3}]}},\"rr\" :{4}}}"; Cookie sid = RequstService.GetCookie("wxsid"); Cookie uin = RequstService.GetCookie("wxuin"); string sync_keys = ""; foreach (KeyValuePair <string, string> p in _syncKey) { sync_keys += "{\"Key\":" + p.Key + ",\"Val\":" + p.Value + "},"; } sync_keys = sync_keys.TrimEnd(','); sync_json = string.Format(sync_json, uin.Value, sid.Value, _syncKey.Count, sync_keys, (long)(DateTime.Now.ToUniversalTime() - new System.DateTime(1970, 1, 1)).TotalMilliseconds, LoginService.sKey); if (sid != null && uin != null) { byte[] bytes = RequstService.SendPostRequest(InterFaceURL._sync_url + sid.Value + "&lang=zh_CN&skey=" + LoginService.sKey + "&pass_ticket=" + LoginService.pass_Ticket, sync_json); string sync_str = Encoding.UTF8.GetString(bytes); JObject sync_resul = JsonConvert.DeserializeObject(sync_str) as JObject; if (sync_resul["SyncKey"]["Count"].ToString() != "0") { _syncKey.Clear(); foreach (JObject key in sync_resul["SyncKey"]["List"]) { if (!_syncKey.ContainsKey(key["Key"].ToString())) { _syncKey.Add(key["Key"].ToString(), key["Val"].ToString()); } } } return(sync_resul); } else { return(null); } }
/// <summary> /// 获取好友列表数据 /// </summary> public static JObject Friends() { try { if (sid != null && uin != null) { string init_json = string.Format("{{\"BaseRequest\":{{\"Uin\":\"{0}\",\"Sid\":\"{1}\",\"Skey\":\"{2}\",\"DeviceID\":\"e1615250492\"}}}}", uin.Value, sid.Value, LoginService.sKey); byte[] bytesFriends = RequstService.SendPostRequest(InterFaceURL._friends_url + "&pass_ticket=" + LoginService.pass_Ticket, init_json); if (bytesFriends == null) { return(null); } string friends_str = Encoding.UTF8.GetString(bytesFriends); friends_result = JsonConvert.DeserializeObject(friends_str) as JObject; return(friends_result); } return(null); } catch (Exception ex) { return(null); } }
/// <summary> /// 获取微信讨论组头像 /// </summary> /// <param name="usename"></param> /// <returns></returns> public Image GetHeadImg(string usename) { byte[] bytes = RequstService.SendGetRequest(InterFaceURL._getheadimg_url + usename); return(Image.FromStream(new MemoryStream(bytes))); }
/// <summary> /// 获取好友头像 /// </summary> /// <param name="username"></param> /// <returns></returns> public Image GetIcon(string username) { byte[] bytes = RequstService.SendGetRequest(InterFaceURL._geticon_url + username); return(Image.FromStream(new MemoryStream(bytes))); }
public String UploadImg(string from, string to, FileStream fileinfo) { Cookie sId = RequstService.GetCookie("wxsid"); Cookie uIn = RequstService.GetCookie("wxuin"); Cookie webwx_data_ticket = RequstService.GetCookie("webwx_data_ticket"); string sKey = LoginService.sKey; String response = ""; Stream inputStream = null; Stream inputStreamReader = null; BufferedStream bufferedReader = null; HttpWebRequest conn = (HttpWebRequest)WebRequest.Create("https://file2.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json"); try { System.Net.ServicePointManager.Expect100Continue = false; //默认是true,所以导致错误 //请求头参数 String boundary = "----WebKitFormBoundary6oVvR66QUmo1TkXD"; //区分每个参数之间 String freFix = "--"; String newLine = "\r\n"; // 请求主体 StringBuilder sb = new StringBuilder(); string str = ""; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"id\""; str += ""; str += "WU_FILE_0"; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"name\""; str += ""; str += "1.png"; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"type\""; str += ""; str += "image / png"; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"lastModifiedDate\""; str += ""; str += "Wed Mar 15 2017 18:13:15 GMT + 0800(中国标准时间)"; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"size\""; str += ""; str += "3195"; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"mediatype\""; str += ""; str += "pic"; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"uploadmediarequest\""; str += ""; str += "{ \"UploadType\":2,\"BaseRequest\":{ \"Uin\":" + uIn + ",\"Sid\":\"" + sId + "\",\"Skey\":\"" + sKey + "\",\"DeviceID\":\"e772864972883278\"},\"ClientMediaId\":" + DateTime.Now.Millisecond + ",\"TotalLen\":3195,\"StartPos\":0,\"DataLen\":3195,\"MediaType\":4,\"FromUserName\":\"" + from + "\",\"ToUserName\":\"" + to + "\",\"FileMd5\":\"7911968c2371ab9b72c3f42d52776ce9\"}"; str += " ------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"webwx_data_ticket\""; str += ""; str += "" + webwx_data_ticket + ""; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"pass_ticket\""; str += ""; str += "" + LoginService.pass_Ticket + ""; str += "------WebKitFormBoundary42gviyclJo9GfHd7"; str += "Content - Disposition: form - data; name = \"filename\"; filename = \"1.png\""; str += "Content - Type: image / png"; str += ""; str += ""; str += " ------WebKitFormBoundary42gviyclJo9GfHd7--"; byte[] data = Encoding.UTF8.GetBytes(str); conn.Method = "POST"; conn.Accept = "*/*"; conn.ContentType = "multipart/form-data;"; conn.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36;"; conn.Referer = "https://wx2.qq.com/?&lang=zh_CN"; conn.Host = "file2.wx.qq.com"; conn.Headers.Add("Accept-Encoding", "gzip, deflate, br"); conn.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8"); conn.Headers.Add("Cache-Control", "no-cache"); //conn.Headers.Add("Connection", "Keep-Alive"); conn.Headers.Add("Origin", "https://wx2.qq.com"); conn.Headers.Add("Pragma", "no-cache"); string url_1 = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json&sid="; string url_2 = "https://file2.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json&sid="; // HttpWebRequest conn11 = (HttpWebRequest)WebRequest.Create(url_1); // // 输入文件流数据 // byte[] buffers11 = new Byte[checked((uint)Math.Min(4096, (int)fileinfo.Length))]; // // string BaseRequest = "{" + // "\"DeviceID\" : \"e441551176\"," + // "\"Sid\" : \"" + sId.Value + "\"," + // "\"Skey\" : \"" + LoginService.sKey + "\"," + // "\"Uin\" : \"" + uIn.Value + "\"}"; // string fdic = "{" + // "\"BaseRequest\" : " + BaseRequest + "," + // "\"ClientMediaId\" : \"" + DateTime.Now.Millisecond + "\"," + // "\"TotalLen\" : \"" + fileinfo.Length + "\"," + // "\"StartPos\" : \"0\"," + // "\"DataLen\" : \"" + fileinfo.Length + "\"," + // "\"MediaType\" : \"4\"}"; // // string files = "{" + // "\"id\": \"WU_FILE_0\"," + // "\"name\": \"temp\"," + // "\"type\": \"image/png\"," + // "\"lastModifiedDate\":\"Wed Mar 15 2017 18:13:15 GMT+0800\"," + // "\"size\": \"" + fileinfo.Length + "\"," + // "\"mediatype\":\"pic\"," + // "\"uploadmediarequest\": " + fdic + "," + // "\"webwx_data_ticket\": \"" + webwx_data_ticket.Value + "\"," + // "\"pass_ticket\": \"" + LoginService.pass_Ticket + "\"," + // "\"filename\": \"\"}"; // byte[] data = Encoding.UTF8.GetBytes(files); byte[] bArr = new byte[fileinfo.Length]; conn.ContentLength = data.Length + bArr.Length; fileinfo.Read(bArr, 0, bArr.Length); fileinfo.Close(); using (Stream datasteam = conn.GetRequestStream()) { datasteam.Write(data, 0, data.Length); datasteam.Write(bArr, 0, bArr.Length); datasteam.Close(); } HttpWebResponse myResponse = conn.GetResponse() as HttpWebResponse; using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"))) { response = reader.ReadToEnd(); } HttpWebResponse response1 = (HttpWebResponse)conn.GetResponse(); Stream response_stream = response1.GetResponseStream(); int count = (int)response1.ContentLength; int offset = 0; byte[] buf = new byte[count]; while (count > 0) //读取返回数据 { int n = response_stream.Read(buf, offset, count); if (n == 0) { break; } count -= n; offset += n; } return(buf.ToString()); if (RequstService.CookiesContainer == null) { RequstService.CookiesContainer = new CookieContainer(); } conn.CookieContainer = RequstService.CookiesContainer; //启用cookie //读取响应信息 inputStream = conn.GetRequestStream(); inputStreamReader = inputStream; bufferedReader = new BufferedStream(inputStreamReader); String strs = ""; StringBuilder buffer = new StringBuilder(); while ((strs = bufferedReader.ToString()) != null) { buffer.Append(strs); } response = buffer.ToString(); } catch (Exception e) { // e.printStackTrace(); } finally { if (conn != null) { // conn.cloas(); } try { bufferedReader.Close(); inputStreamReader.Close(); inputStream.Close(); } catch (IOException execption) { } } return(response); }