国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

微信開發 消息推送實現代碼

2024-05-06 16:32:32
字體:
來源:轉載
供稿:網友

最近做微信公共號的開發,有個需求是這樣的消息推送,以文本的形式把編輯的消息發送給微信企業號中的某一個應用組,這里做下筆記,以下是整理內容:

//定義數據模型 public class Access_token{public Access_token(){//// TODO: 在此處添加構造函數邏輯//}string _access_token;string _expires_in; ///  /// 獲取到的憑證/// public string access_token{get { return _access_token; }set { _access_token = value; }}  ///  /// 憑證有效時間,單位:秒/// public string expires_in{get { return _expires_in; }  set { _expires_in = value; }} }
 public ActionResult index(string returnUrl){ GetAccess_token(); IsExistAccess_Token(); return View();}   public static Access_token GetAccess_token(){string AppUrl = "https://qyapi.weixin.qq.com/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["access_token"].ToString();string expires_in = jObj["expires_in"].ToString(); Access_token mode = new Access_token();mode.access_token = token;mode.expires_in = expires_in;return mode;} /// 

根據當前日期 判斷Access_Token 是否超期 如果超期返回新的Access_Token 否則返回之前的Access_Token

public static string IsExistAccess_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("Access_Token").InnerText;YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);  if (DateTime.Now > YouXRQ){DateTime _youxrq = DateTime.Now;Access_token mode = GetAccess_token();xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;_youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in));xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();xml.Save(filepath);Token = mode.access_token;} object text = new{toparty = "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.com/cgi-bin/message/send?access_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-www-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; }

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌兰浩特市| 威远县| 集贤县| 南雄市| 库尔勒市| 民丰县| 寻乌县| 上虞市| 洱源县| 天气| 鄢陵县| 渝北区| 雷山县| 清涧县| 神农架林区| 永川市| 颍上县| 饶阳县| 瑞丽市| 大石桥市| 娄烦县| 夏邑县| 原平市| 获嘉县| 都江堰市| 云龙县| 教育| 高雄市| 手机| 新巴尔虎右旗| 二手房| 开平市| 连江县| 东乌珠穆沁旗| 颍上县| 大新县| 绵阳市| 布尔津县| 藁城市| 屏东市| 沙河市|