using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Net; using System.Text; using System.Threading; using System.Web; using WX.CRM.BLL.TS; using WX.CRM.BLL.Util; using WX.CRM.Common; namespace WX.CRM.CRMServices.TS { public class TSMsgSend { public void StartPush() { WX_TS_QUNWORALIAS_BL bl = new WX_TS_QUNWORALIAS_BL(); string clientid = Utility.GetSettingByKey("CRMClientKey"); WX.CRM.IBLL.Util.ISecurityHelper sHelper = new SecurityHelper(); string url = "https://console.tim.qq.com/v4/openim/sendmsg?usersig={0}&identifier={1}&sdkappid={2}&random={3}&contenttype=json"; //用户签名 string usersig = ConfigurationManager.AppSettings["yunim_usersig"] != null ? ConfigurationManager.AppSettings["yunim_usersig"].ToString() : "eJx1z09PgzAYx-E7r6LhitH*ATZMPJABRt3ColuyemmatmDjgApVBON7N*KScfG5-j7JN8*XAwBwd*unSy5E815bZgejXHANXAyxH7gXZ2CMloxbRlo5AeRDCDEJwrlSn0a3ivHCqvZPBYtwCX9vprRUtdWFPhkuK12j2d7JVzb1-g91upzGTbpf3SWrt30ncv8QC*r5tt4IKoexwCKJPJpVPHvEfdkMVy*eiXUa98tjPN4e05wsghLqQ0Dxfb99zvnQ1H31QOCYRclH26*puJklra7U6SdEMApRhF3n2-kBGp1YWA__"; string identifier = ConfigurationManager.AppSettings["yunim_identifier"] != null ? ConfigurationManager.AppSettings["yunim_identifier"].ToString() : "admin1"; string sdkappid = ConfigurationManager.AppSettings["yunim_appid"] != null ? ConfigurationManager.AppSettings["yunim_appid"].ToString() : "1400023565"; string random2 = new Random().Next(1000000, 2000000).ToString(); url = string.Format(url, usersig, identifier, sdkappid, random2); DataTable table = bl.GetNeedSend();//获取批次 foreach (DataRow row in table.Rows) { decimal pici = Convert.ToDecimal(row["PICI"]); string alias = row["alias"].ToString().Replace("-", "_").ToLower(); string content = HttpUtility.UrlEncode(row["MSG"].ToString(), Encoding.UTF8); ; long random = new Random().Next(1000000, 2000000); var msgobj = new { SyncOtherMachine = 1, //消息同步至发送方 From_Account = "admin1", To_Account = alias + "_yun", MsgRandom = random, MsgTimeStamp = (long)Utility.ConvertDateTimeInt(DateTime.Now), MsgBody = new List() { new { MsgType= "TIMTextElem", MsgContent=new {Text= "{\"pici\":"+pici+",\"alias\":\""+alias+"\",\"message\":\""+content+"\"}"} } } }; var para = JsonConvert.SerializeObject(msgobj); WebClient webClient = new WebClient(); webClient.Encoding = Encoding.UTF8; webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; //string result = webClient.UploadString(url, "POST", "{\"jobwxusername\" : \"Ashu|29|7 Years|.NET\"}"); string result = webClient.UploadString(url, "POST", para); ResultMsg resultMsg = JsonConvert.DeserializeObject(result); DateTime dateTime = Utility.ConvertIntDateTime(resultMsg.MsgTime); int isOk = 2; if (resultMsg.ActionStatus.ToUpper() == "OK") { isOk = 1; bl.UpdatePiciStatus(pici, isOk, result); } else { bl.UpdatePiciStatus(pici, isOk, result); } Thread.Sleep(200); } } } }