/// <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 = "{{" + "\"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 = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.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 = BaseService.SendPostRequest(_sendmsg_url + sid.Value + "&lang=zh_CN&pass_ticket=" + LoginService.Pass_Ticket, msg_json); string send_result = Encoding.UTF8.GetString(bytes); } }
/// <summary> /// 微信初始化 /// </summary> /// <returns></returns> public JObject WxInit() { string init_json = "{{\"BaseRequest\":{{\"Uin\":\"{0}\",\"Sid\":\"{1}\",\"Skey\":\"\",\"DeviceID\":\"e1615250492\"}}}}"; Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); if (sid != null && uin != null) { init_json = string.Format(init_json, uin.Value, sid.Value); byte[] bytes = BaseService.SendPostRequest(_init_url + "&pass_ticket=" + LoginService.Pass_Ticket, init_json); string init_str = Encoding.UTF8.GetString(bytes); JObject init_result = JsonConvert.DeserializeObject(init_str) as JObject; foreach (JObject synckey in init_result["SyncKey"]["List"]) //同步键值 { _syncKey.Add(synckey["Key"].ToString(), synckey["Val"].ToString()); } return(init_result); } else { return(null); } }
/// <summary> /// 微信初始化 /// </summary> /// <returns></returns> public JObject WxInit() { //string init_json = "{{\"BaseRequest\":{{\"Uin\":\"{0}\",\"Sid\":\"{1}\",\"Skey\":\"\",\"DeviceID\":\"e1615250492\"}}}}"; string init_json = "{{\"BaseRequest\":{{\"Uin\":\"{0}\",\"Sid\":\"{1}\",\"Skey\":\"\",\"DeviceID\":\"e2324132323\"}}}}"; string init_url = ""; Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); if (sid != null && uin != null) { init_json = string.Format(init_json, uin.Value, sid.Value); init_url = string.Format(_init_url, (long)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalMilliseconds); byte[] bytes = BaseService.SendPostRequest(_init_url + "&pass_ticket=" + LoginService.Pass_Ticket, init_json); string init_str = Encoding.UTF8.GetString(bytes); log.Debug("Login Response: \n" + init_str); JObject init_result = JsonConvert.DeserializeObject(init_str) as JObject; if (init_result["SyncKey"]["Count"].ToString() != "0") { _syncKey.Clear(); foreach (JObject synckey in init_result["SyncKey"]["List"]) //同步键值 { _syncKey.Add(synckey["Key"].ToString(), synckey["Val"].ToString()); } } return(init_result); } else { return(null); } }
/// <summary> /// 微信同步检测 /// </summary> /// <returns></returns> public string WxSyncCheck() { 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 = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); if (sid != null && uin != null) { _synccheck_url = string.Format(_synccheck_url, sid.Value, uin.Value, sync_key, (long)(DateTime.Now.ToUniversalTime() - new System.DateTime(1970, 1, 1)).TotalMilliseconds, LoginService.SKey.Replace("@", "%40"), "e1615250492"); byte[] bytes = BaseService.SendGetRequest(_synccheck_url + "&_=" + DateTime.Now.Ticks); if (bytes != null) { return(Encoding.UTF8.GetString(bytes)); } else { return(null); } } else { return(null); } }
/// <summary> /// 移除群聊成员 /// </summary> /// <param name="roomName">房间名</param> /// <param name="userNames">成员</param> /// <returns></returns> public JObject DeleteMember(string roomName, List <string> userNames) { Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); byte[] bytes = BaseService.SendPostRequest(string.Format(_delmember_url, LoginService.Pass_Ticket) , string.Format(_delmem_msg_json, uin.Value, sid.Value, LoginService.SKey, roomName , string.Join(",", userNames))); string delmem_str = Encoding.UTF8.GetString(bytes); return(JsonConvert.DeserializeObject(delmem_str) as JObject); }
/// <summary> /// 发送pic图片 /// </summary> /// <param name="ToUserName"></param> /// <param name="MediaId"></param> public void Wx_SendPicByMediaId(string ToUserName, string MediaId) { Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); string MsgType = "3"; string Content = ""; string ClientMsgId = DateTime.Now.Millisecond.ToString(); string url = "https://" + LoginService.Url + ".qq.com/cgi-bin/mmwebwx-bin/webwxsendmsgimg?fun=async&f=json&lang=zh_CN&pass_ticket=" + LoginService.Pass_Ticket; string data = "{\"BaseRequest\":{\"Uin\":" + uin.Value + ",\"Sid\":\"" + sid.Value + "\",\"Skey\":\"" + LoginService.SKey + "\",\"DeviceID\":\"" + "e831645047756137" + "\"},\"Msg\":{\"Type\":" + MsgType + ",\"MediaId\":\"" + MediaId + "\",\"Content\":\"" + Content + "\",\"FromUserName\":\"" + frmMain.usernameid + "\",\"ToUserName\":\"" + ToUserName + "\",\"LocalID\":\"" + ClientMsgId + "\",\"ClientMsgId\":\"" + ClientMsgId + "\"},\"Scene\":0}"; byte [] bytes = BaseService.SendPostRequest(url, data); string send_result = Encoding.UTF8.GetString(bytes); }
/// <summary> /// 创建群聊 /// </summary> /// <param name="userNames">群成员</param> /// <returns></returns> public JObject CreateRoom(List <string> userNames) { Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); byte[] bytes = BaseService.SendPostRequest(string.Format(_addroom_url, LoginService.Pass_Ticket , (long)(DateTime.Now.ToUniversalTime() - new System.DateTime(1970, 1, 1)).TotalMilliseconds) , string.Format(_addroom_msg_json, uin.Value, sid.Value, LoginService.SKey, userNames.Count() , JsonConvert.SerializeObject(userNames.Select(c => new { UserName = c }).ToList(), Formatting.None))); string room_str = Encoding.UTF8.GetString(bytes); return(JsonConvert.DeserializeObject(room_str) as JObject); }
/// <summary> /// 发送动态图 /// </summary> /// <param name="ToUserName"></param> /// <param name="MediaId"></param> public void Wx_SendGifByMediaId(string ToUserName, string MediaId) { Random ran = new Random(); int RandKey = ran.Next(1000, 9999); Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); string MsgType = "47"; string EmojiFlag = "2"; string ClientMsgId = ((long)((DateTime.Now.ToUniversalTime() - new System.DateTime(1970, 1, 1)).TotalMilliseconds)).ToString() + RandKey.ToString(); string url = "https://" + LoginService.Url + ".qq.com/cgi-bin/mmwebwx-bin/webwxsendemoticon?fun=sys&lang=zh_CN&pass_ticket=" + LoginService.Pass_Ticket; string data = "{\"BaseRequest\":{\"Uin\":" + uin.Value + ",\"Sid\":\"" + sid.Value + "\",\"Skey\":\"" + LoginService.SKey + "\",\"DeviceID\":\"" + "e831645047756137" + "\"},\"Msg\":{\"Type\":" + MsgType + ",\"EmojiFlag\":" + EmojiFlag + ",\"MediaId\":\"" + MediaId + "\",\"FromUserName\":\"" + frmMain.usernameid + "\",\"ToUserName\":\"" + ToUserName + "\",\"LocalID\":\"" + ClientMsgId + "\",\"ClientMsgId\":\"" + ClientMsgId + "\"},\"Scene\":0}"; byte[] bytes = BaseService.SendPostRequest(url, data); string send_result = Encoding.UTF8.GetString(bytes); }
/// <summary> /// 获取群聊成员列表 /// </summary> /// <returns></returns> public JObject GetGroupItem(string groupid) { Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); if (sid != null && uin != null) { string data = "{\"List\":[{\"UserName\":\"" + groupid + "\",\"EncryChatRoomId\":\"\"}],\"Count\":1,\"BaseRequest\":{\"Uin\":" + uin.Value + ",\"Sid\":\"" + sid.Value + "\",\"Skey\":\"" + LoginService.SKey + "\",\"DeviceID\":\"" + "e831645047756137" + "\"}}"; string urll = _group_url + "&r=" + (long)(DateTime.Now.ToUniversalTime() - new System.DateTime(1970, 1, 1)).TotalMilliseconds + "&pass_ticket=" + LoginService.Pass_Ticket; byte[] bytes1 = BaseService.SendPostRequest(urll, data); string contact_str = Encoding.UTF8.GetString(bytes1); return(JsonConvert.DeserializeObject(contact_str) as JObject); } else { return(null); } }
/// <summary> /// 微信同步 /// </summary> /// <returns></returns> public JObject WxSync() { string sync_json = "{{\"BaseRequest\" : {{\"DeviceID\":\"e1615250492\",\"Sid\":\"{1}\", \"Skey\":\"{5}\", \"Uin\":\"{0}\"}},\"SyncKey\" : {{\"Count\":{2},\"List\":[{3}]}},\"rr\" :{4}}}"; Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.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 = BaseService.SendPostRequest(_sync_url + sid.Value + "&lang=zh_CN&skey=" + LoginService.SKey + "&pass_ticket=" + LoginService.Pass_Ticket, sync_json); string sync_str = Encoding.UTF8.GetString(bytes); log.Debug("SynResString:" + sync_str + "\n"); JObject sync_resul = JsonConvert.DeserializeObject(sync_str) as JObject; if (sync_resul["BaseResponse"]["Ret"].ToString() != "0") { log.Debug("Sync Failed With code " + sync_resul["BaseResponse"]["Ret"].ToString() + " \n"); return(null); } if (sync_resul["SyncKey"]["Count"].ToString() != "0") { _syncKey.Clear(); foreach (JObject key in sync_resul["SyncKey"]["List"]) { log.Debug("SynRes:" + key["Key"].ToString() + ":" + key["Val"].ToString() + "\n"); _syncKey.Add(key["Key"].ToString(), key["Val"].ToString()); } } return(sync_resul); } else { return(null); } }
/// <summary> /// 微信同步检测 /// </summary> /// <returns></returns> public JObject WxSyncCheck() { string sync_key = ""; List <string> parts = new List <string>(); foreach (KeyValuePair <string, string> p in _syncKey) { parts.Add(p.Key + "_" + p.Value); } sync_key = string.Join("%7C", parts); Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); if (sid != null && uin != null) { log.Debug("sync_key " + sync_key + "\n"); string synccheck_url = string.Format(_synccheck_url, sid.Value, uin.Value, sync_key, (long)(DateTime.Now.ToUniversalTime() - new System.DateTime(1970, 1, 1)).TotalMilliseconds, LoginService.SKey.Replace("@", "%40"), "e1615250492"); log.Debug("synccheck_url " + synccheck_url + "\n"); byte[] bytes = BaseService.SendGetRequest(synccheck_url + "&_=" + DateTime.Now.Ticks); if (bytes != null) { string s = Encoding.UTF8.GetString(bytes); log.Debug("synccheckResponse:\n" + s); s = s.Replace("window.synccheck=", ""); JObject synccheck_resul = JsonConvert.DeserializeObject(s) as JObject; return(synccheck_resul); } else { log.Debug("synccheckResponse is null\n"); return(null); } } else { return(null); } }
public static string upload(sendMainForm cmsForm, string msg, string from, string to, string fullPath) { fullPath = fullPath.Replace("%20", " "); string FileMd5 = LogUtil.GetMD5HashFromFile(fullPath); string FileSize = "" + LogUtil.FileSize(fullPath); string filename = System.IO.Path.GetFileName(fullPath);//文件名 “Default.aspx” string lastModifiedDate = "Tue Dec 27 2016 23:30:08 GMT+0800 (CST)"; //lastModifiedDate = new FileInfo(fullPath).LastWriteTime.ToString(); NameValueCollection data = new NameValueCollection(); data.Add("id", "WU_FILE_0"); data.Add("name", filename); data.Add("type", "image/jpeg"); data.Add("lastModifiedDate", lastModifiedDate); data.Add("size", "" + FileSize); data.Add("mediatype", "pic"); string msg_json = "{{" + "\"UploadType\":2," + "\"BaseRequest\":" + "{{" + "\"Uin\":{1}," + "\"Sid\":\"{0}\"," + "\"Skey\":\"{6}\"," + "\"DeviceID\":\"e441551176\"" + "}}," + "\"ClientMediaId\":{8}," + "\"TotalLen\":{11}," + "\"StartPos\":0," + "\"DataLen\":{11}," + "\"MediaType\":4," + "\"FromUserName\":\"{3}\"," + "\"ToUserName\":\"{4}\"," + "\"FileMd5\":\"{10}\"" + "}}"; Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); Cookie webwx_data_ticket = BaseService.GetCookie("webwx_data_ticket"); if (sid != null && uin != null && webwx_data_ticket != null) { msg_json = string.Format(msg_json , sid.Value , uin.Value , msg , from , to , "4" , LoginService.SKey , DateTime.Now.Millisecond , DateTime.Now.Millisecond , DateTime.Now.Millisecond , FileMd5 , FileSize); data.Add("uploadmediarequest", msg_json); } data.Add("webwx_data_ticket", webwx_data_ticket.Value); data.Add("pass_ticket", LoginService.Pass_Ticket); //LogUtil.log_call(cmsForm, "lastModifiedDate:" + lastModifiedDate); //LogUtil.log_call(cmsForm, "msg_json:" + msg_json); //LogUtil.log_call(cmsForm, "webwx_data_ticket:" + webwx_data_ticket); //LogUtil.log_call(cmsForm, "FileMd5:" + FileMd5); //LogUtil.log_call(cmsForm, "FileSize:" + FileSize); //LogUtil.log_call(cmsForm, "filename:" + filename); string content = BaseService.HttpUploadFile(_post_url, new string[] { fullPath }, data); //return content; JObject sync_resul = JsonConvert.DeserializeObject(content) as JObject; return(sync_resul["MediaId"].ToString()); //Console.WriteLine(); //Console.ReadKey(); }
/// <summary> /// /// 发送图片,png格式 /// </summary> /// <param name="ToUserName"></param> /// <param name="filePath"></param> public void Wx_SendFile(string ToUserName, string filePath) { Cookie sid = BaseService.GetCookie("wxsid"); Cookie uin = BaseService.GetCookie("wxuin"); Cookie webwx_data_ticket = BaseService.GetCookie("webwx_data_ticket"); string[] fileTypes = new string[] { "image/jpeg", "image/png", "image/bmp", "image/jpeg", "text/plain", "application/msword", "application/vnd.ms-excel" }; string[] mediaTypes = new string[] { "pic", "pic", "pic", "doc", "doc", "doc" }; //string ClientMsgId = DateTime.Now.Millisecond.ToString(); long ClientMsgId = (long)(DateTime.Now.ToUniversalTime() - new System.DateTime(1970, 1, 1)).TotalMilliseconds; FileInfo file = new FileInfo(filePath); if (!file.Exists) { return; } string fileMd5 = GetMD5HashFromFile(file.FullName); byte[] fileData = File.ReadAllBytes(file.FullName); #region data string data = @"------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""id"" WU_FILE_0 ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""name"" " + file.FullName + @" ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""type"" " + "image/png" + @" ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""lastModifiedDate"" " + DateTime.Now.ToString("r").Replace(",", "") + @"+0800 ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""size"" " + fileData.Length.ToString() + @" ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""mediatype"" " + "pic" + @" ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""uploadmediarequest"" {""UploadType"":2,""BaseRequest"":{""Uin"":" + uin.Value + @",""Sid"":""" + sid.Value + @""",""Skey"":""" + LoginService.SKey + @""",""DeviceID"":""" + "e831645047756137" + @"""},""ClientMediaId"":" + ClientMsgId + @",""TotalLen"":" + fileData.Length.ToString() + @",""StartPos"":0,""DataLen"":" + fileData.Length.ToString() + @",""MediaType"":4,""FromUserName"":""" + frmMain.usernameid + @""",""ToUserName"":""" + ToUserName + @""",""FileMd5"":""" + fileMd5 + @"""} ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""webwx_data_ticket"" " + webwx_data_ticket.Value + @" ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""pass_ticket"" " + LoginService.Pass_Ticket + @" ------WebKitFormBoundaryqmAlcppnh4tFP6al Content-Disposition: form-data; name=""filename""; filename=""" + file.Name + @""" Content-Type: " + "image/png" + @" "; #endregion byte[] postData = Encoding.UTF8.GetBytes(data); byte[] endData = Encoding.UTF8.GetBytes("\r\n------WebKitFormBoundaryqmAlcppnh4tFP6al--\r\n"); List <byte[]> lt = new List <byte[]>(); lt.Add(postData); lt.Add(fileData); lt.Add(endData); byte[] tmp = new byte[postData.Length + fileData.Length]; System.Buffer.BlockCopy(postData, 0, tmp, 0, postData.Length); System.Buffer.BlockCopy(fileData, 0, tmp, postData.Length, fileData.Length); byte[] body = new byte[tmp.Length + endData.Length]; System.Buffer.BlockCopy(tmp, 0, body, 0, tmp.Length); System.Buffer.BlockCopy(endData, 0, body, tmp.Length, endData.Length); string url = _image_url; byte[] bytes = BaseService.SendPostRequestByByte(url, body, "----WebKitFormBoundaryqmAlcppnh4tFP6al"); string send_result = Encoding.UTF8.GetString(bytes); if (send_result.IndexOf("\"Ret\": 1,") > 0) { return; } /////string ret = http.PostBytes(url, lt, "https://" + info.fun + ".qq.com/", "*/*", "multipart/form-data; boundary=----WebKitFormBoundaryqmAlcppnh4tFP6al"); ///// /////Hashtable json = (Hashtable)ClsJson.Decode(ret); /////if (json == null) /////{ ///// return; /////} string MediaId = send_result.Substring(send_result.IndexOf("\"MediaId\":"), send_result.IndexOf("\"StartPos\":") - send_result.IndexOf("\"MediaId\":")).Replace("\"MediaId\": \"", "").Replace("\",", "").Replace(" ", "").Replace("\n", ""); //SendMsg(MediaId, frmMain.usernameid, ToUserName,3); Wx_SendPicByMediaId(ToUserName, MediaId); }