微信开发 消息推送实现代码
网络编程 2021-07-04 20:01www.168986.cn编程入门
这篇文章主要介绍了微信开发 消息推送实现代码的相关资料,需要的朋友可以参考下
最近做微信公共号的开发,有个需求是这样的消息推送,以文本的形式把编辑的消息发送给微信企业号中的某一个应用组,这里做下笔记,以下是整理内容:
//定义数据模型 public class Aess_token { public Aess_token() { // // TODO: 在此处添加构造函数逻辑 // } string _aess_token; string _expires_in; /// /// 获取到的凭证 /// public string aess_token { get { return _aess_token; } set { _aess_token = value; } } /// /// 凭证有效时间,单位:秒 /// public string expires_in { get { return _expires_in; } set { _expires_in = value; } } }
public ActionResult index(string returnUrl) { GetAess_token(); IsExistAess_Token(); return View(); } public static Aess_token GetAess_token() { string AppUrl = "https://qyapi.weixin.qq./cgi-bin/gettoken?"; string AppID = "应用组的CorpID";//在设置-》权限管理-》系统管理组 string AppSecret = "应用组的Secret";//在设置-》权限管理-》系统管理组 WebClient webClient = new WebClient(); Byte[] bytes = webClient.DownloadData(string.Format("{0}corpid={1}&corpsecret={2}", AppUrl, AppID, AppSecret)); string result = Encoding.GetEncoding("utf-8").GetString(bytes); JObject jObj = JObject.Parse(result); string token = jObj["aess_token"].ToString(); string expires_in = jObj["expires_in"].ToString(); Aess_token mode = new Aess_token(); mode.aess_token = token; mode.expires_in = expires_in; return mode; } ///
根据当前日期 判断Aess_Token 是否超期 如果超期返回新的Aess_Token 否则返回之前的Aess_Token
public static string IsExistAess_Token() { string Token = string.Empty; DateTime YouXRQ; string strPath = "../../weixin/XMLFile.xml"; // 读取XML文件中的数据,并显示出来 //string filepath = System.Web.Hosting.HostingEnvironment.MapPath(strPath); string filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8); XmlDocument xml = new XmlDocument(); xml.Load(str); str.Close(); str.Dispose(); Token = xml.SelectSingleNode("xml").SelectSingleNode("Aess_Token").InnerText; YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Aess_YouXRQ").InnerText); if (DateTime.Now > YouXRQ) { DateTime _youxrq = DateTime.Now; Aess_token mode = GetAess_token(); xml.SelectSingleNode("xml").SelectSingleNode("Aess_Token").InnerText = mode.aess_token; _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in)); xml.SelectSingleNode("xml").SelectSingleNode("Aess_YouXRQ").InnerText = _youxrq.ToString(); xml.Save(filepath); Token = mode.aess_token; } object text = new { arty = "1", agentid = "2", msgtype = "text", text = new { content = "项目名称:"+来保网+"" } }; string wcr= btnSend(Token, text); return wcr; } public static string btnSend(string Token, object text) { string url = "https://qyapi.weixin.qq./cgi-bin/message/send?aess_token=" + Token; WebRequest req = WebRequest.Create(url); JavaScriptSerializer aa = new JavaScriptSerializer(); string postData = aa.Serialize(text); byte[] requestBytes = Encoding.UTF8.GetBytes(postData); req.Method = "POST"; req.ContentType = "application/x--form-urlencoded"; req.ContentLength = requestBytes.Length; Stream requestStream = req.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.Default); string backstr = sr.ReadToEnd(); sr.Close(); res.Close(); WeChatReturn WCR = aa.Deserialize(backstr); return WCR.errmsg; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程