小編給大家分享一下微信開發(fā)之如何實現(xiàn)推送消息的,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)主營雷州網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,App定制開發(fā),雷州h5小程序設計搭建,雷州網(wǎng)站營銷推廣歡迎雷州等地區(qū)企業(yè)咨詢最近做微信公共號的開發(fā),有個需求是這樣的消息推送,以文本的形式把編輯的消息發(fā)送給微信企業(yè)號中的某一個應用組,這里做下筆記,以下是整理內(nèi)容:
//定義數(shù)據(jù)模型 public class Access_token { public Access_token() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } 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 = "/tupian/20230522/gettoken string AppID = "應用組的CorpID";//在設置-》權(quán)限管理-》系統(tǒng)管理組 string AppSecret = "應用組的Secret";//在設置-》權(quán)限管理-》系統(tǒng)管理組 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; } ///
根據(jù)當前日期 判斷Access_Token 是否超期 如果超期返回新的Access_Token 否則返回之前的Access_Token
public static string IsExistAccess_Token() { string Token = string.Empty; DateTime YouXRQ; string strPath = "../../weixin/XMLFile.xml"; // 讀取XML文件中的數(shù)據(jù),并顯示出來 //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 = "項目名稱:"+來保網(wǎng)+"" } }; string wcr= btnSend(Token, text); return wcr; } public static string btnSend(string Token, object text) { string url = "/tupian/20230522/send 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; }
以上是“微信開發(fā)之如何實現(xiàn)推送消息的”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享名稱:微信開發(fā)之如何實現(xiàn)推送消息的-創(chuàng)新互聯(lián)
當前路徑:http://muchs.cn/article10/phido.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、網(wǎng)站設計、移動網(wǎng)站建設、營銷型網(wǎng)站建設、App開發(fā)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容