/// <summary> /// 微信同步检测(检查是否有新消息) /// </summary> /// <returns></returns> public static string WxSyncCheck() { string datetime = LoginService.GetTimeStamp(); string sync_key = ""; foreach (KeyValuePair <string, string> p in _syncKey) { sync_key += p.Key + "_" + p.Value + "%7C"; } sync_key = sync_key.TrimEnd('%', '7', 'C'); Cookie sid = RequstService.GetCookie("wxsid"); Cookie uin = RequstService.GetCookie("wxuin"); if (sid != null && uin != null) { //同步检查url string _synccheckUrl = InterFaceURL._synccheck_url + string.Format("&sid={0}&uin={1}&synckey={2}&skey={3}&deviceid={4}", sid.Value, uin.Value, sync_key, LoginService.sKey, "e1615250492"); byte[] bytes = RequstService.SendGetRequest(_synccheckUrl + "&pass_ticket=" + LoginService.pass_Ticket + "&_=" + datetime); if (bytes != null) { return(Encoding.UTF8.GetString(bytes)); } else { return(null); } } else { return(null); } }
/// <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 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); } }
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&sid="); try { System.Net.ServicePointManager.Expect100Continue = false; //默认是true,所以导致错误 //请求头参数 String boundary = "----WebKitFormBoundary6oVvR66QUmo1TkXD"; //区分每个参数之间 String freFix = "--"; String newLine = "\r\n"; // 请求主体 StringBuilder sb = new StringBuilder(); sb.Append(freFix + boundary).Append(newLine); //这里注意多了个freFix,来区分去请求头中的参数 sb.Append("Content-Disposition: form-data; name=\"id\""); sb.Append(newLine).Append(newLine); sb.Append(LoginService.GetTimeStamp()).Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"name\""); sb.Append(newLine).Append(newLine); sb.Append(fileinfo.Name.Substring(fileinfo.Name.LastIndexOf("\\") + 1)).Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"type\""); sb.Append(newLine).Append(newLine); sb.Append("image/png").Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"lastModifiedDate\""); sb.Append(newLine).Append(newLine); sb.Append("Wed Mar 15 2017 18:13:15 GMT+0800").Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"size\""); sb.Append(newLine).Append(newLine); sb.Append(fileinfo.Length).Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"mediatype\""); sb.Append(newLine).Append(newLine); sb.Append("pic").Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"uploadmediarequest\""); sb.Append(newLine).Append(newLine); sb.Append("{ \"UploadType\":2,\"BaseRequest\":{\"Uin\":" + uIn.Value + ",\"Sid\":\"" + sId.Value + "\",\"Skey\":\"" + sKey + "\",\"DeviceID\"://\"e823469202135602\"},\"ClientMediaId\": " + LoginService.GetTimeStamp() + ",\"TotalLen\":" + fileinfo.Length + ",\"StartPos\":0,\"DataLen/\":" + fileinfo.Length + ",\"MediaType\":4,\"FromUserName\":\"" + from + "\",\"ToUserName\":\"" + to + "\",\"FileMd5\":/\"7911968c2371ab9b72c3f42d52776ce9/\"}").Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"webwx_data_ticket\""); sb.Append(newLine).Append(newLine); sb.Append(webwx_data_ticket.Value).Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"pass_ticket\""); sb.Append(newLine).Append(newLine); sb.Append(LoginService.pass_Ticket).Append(newLine); sb.Append(freFix + boundary).Append(newLine); sb.Append("Content-Disposition: form-data; name=\"filename\"; filename=\"" + fileinfo.Name.Substring(fileinfo.Name.LastIndexOf("\\") + 1) + "\""); sb.Append(newLine); sb.Append("Content-Type: image/png"); sb.Append(newLine).Append(newLine); sb.Append(freFix + boundary + freFix); conn.Method = "POST"; conn.Accept = "*/*"; conn.ContentType = "multipart/form-data; boundary=" + boundary; 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"); byte[] data = Encoding.UTF8.GetBytes(sb.ToString()); // 输入文件流数据 byte[] buffers = new Byte[checked ((uint)Math.Min(4096, (int)fileinfo.Length))]; conn.ContentLength = data.Length + buffers.Length; int bytesRead = 0; using (Stream reqStream = conn.GetRequestStream()) { reqStream.Write(data, 0, data.Length); while ((bytesRead = fileinfo.Read(buffers, 0, buffers.Length)) != 0) { reqStream.Write(buffers, 0, bytesRead); } reqStream.Close(); } HttpWebResponse resp = (HttpWebResponse)conn.GetResponse(); Stream stream = resp.GetResponseStream(); string result; //获取响应内容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return(result); 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); // conn11.Method = "POST"; // conn11.Accept = "*/*"; // conn11.ContentType = "multipart/form-data; boundary=" + boundary; // conn11.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"; // conn11.Referer = "https://wx2.qq.com/?&lang=zh_CN"; // conn11.Host = "file2.wx.qq.com"; // conn11.Headers.Add("Accept-Encoding", "gzip, deflate, br"); // conn11.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8"); // conn11.Headers.Add("Cache-Control", "no-cache"); // //conn.Headers.Add("Connection", "Keep-Alive"); // conn11.Headers.Add("Origin", "https://wx2.qq.com"); // conn11.Headers.Add("Pragma", "no-cache"); // // // // 输入文件流数据 // 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[] data11 = Encoding.UTF8.GetBytes(files.ToString()); // conn11.ContentLength = data11.Length + buffers11.Length; // int bytesRead = 0; // using (Stream reqStream = conn11.GetRequestStream()) // { // reqStream.Write(data11, 0, data11.Length); // // while ((bytesRead = fileinfo.Read(buffers11, 0, buffers11.Length)) != 0) // reqStream.Write(buffers11, 0, bytesRead); // // reqStream.Close(); // } // // HttpWebResponse resp = (HttpWebResponse)conn11.GetResponse(); // Stream stream = resp.GetResponseStream(); // string result; // //获取响应内容 // using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) // { // result = reader.ReadToEnd(); // } // return result; // byte[] bytesss = RequstService.SendPostRequest(_upload_image_url + sId.Value + "&lang=zh_CN&pass_ticket=" + LoginService.pass_Ticket, files); // string send_result = Encoding.UTF8.GetString(bytesss); // Stream outputStream = conn.GetRequestStream(); // outputStream.Write(System.Text.Encoding.UTF8.GetBytes(sb.ToString()), 0, 0);//写入请求参数 // BinaryReader dis = new BinaryReader(fileinfo); // int bytes = 0; // byte[] bufferOut = new byte[1024]; // while ((bytes = dis.Read(bufferOut, 1, 1)) != -1) // { // outputStream.Write(bufferOut, 0, bytes);//写入图片 // } // // outputStream.Write(System.Text.Encoding.UTF8.GetBytes(newLine), 0, 0); // outputStream.Write(System.Text.Encoding.UTF8.GetBytes((freFix + boundary + freFix + newLine)), 0, 0);//标识请求数据写入结束 // // dis.Close(); // outputStream.Close(); // 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 str = ""; StringBuilder buffer = new StringBuilder(); while ((str = bufferedReader.ToString()) != null) { buffer.Append(str); } 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); }
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); }