您好,登錄后才能下訂單哦!
小編給大家分享一下使用C#微信接口實現推送模板消息功能的案例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體如下:
public string SendTempletMessge() { string strReturn = string.Empty; try { #region 獲取access_token string apiurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret"; WebRequest request = WebRequest.Create(@apiurl); request.Method = "POST"; WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); Encoding encode = Encoding.UTF8; StreamReader reader = new StreamReader(stream, encode); string detail = reader.ReadToEnd(); var jd = JsonConvert.DeserializeObject<WXApi>(detail); string token = (String)jd.access_token; #endregion #region 組裝信息推送,并返回結果(其它模版消息于此類似) string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token; string temp = "{\"touser\": \"" + UserInfo.OpenID + "\"," + "\"template_id\": \"f3kRRjJeyLDf4tndtg-OJeRvgEdgjjDxCy4T9kuwM70\", " + "\"topcolor\": \"#FF0000\", " + "\"data\": " + "{\"first\": {\"value\": \"您好,您有一條回款通知信息\"}," + "\"keyword1\": { \"value\": \"單位名稱\"}," + "\"keyword2\": { \"value\": \"日期\"}," + "\"keyword3\": { \"value\": \"金額\"}," + "\"keyword4\": { \"value\": \"業務員\"}," + "\"remark\": {\"value\": \"\" }}}"; #endregion //核心代碼 GetResponseData(temp, @url); strReturn = "推送成功"; } catch (Exception ex) { strReturn = ex.Message; } return strReturn; } /// <summary> /// 返回JSon數據 /// </summary> /// <param name="JSONData">要處理的JSON數據</param> /// <param name="Url">要提交的URL</param> /// <returns>返回的JSON處理字符串</returns> public string GetResponseData(string JSONData, string Url) { byte[] bytes = Encoding.UTF8.GetBytes(JSONData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentLength = bytes.Length; request.ContentType = "json"; Stream reqstream = request.GetRequestStream(); reqstream.Write(bytes, 0, bytes.Length); //聲明一個HttpWebRequest請求 request.Timeout = 90000; //設置連接超時時間 request.Headers.Set("Pragma", "no-cache"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream streamReceive = response.GetResponseStream(); Encoding encoding = Encoding.UTF8; StreamReader streamReader = new StreamReader(streamReceive, encoding); string strResult = streamReader.ReadToEnd(); streamReceive.Dispose(); streamReader.Dispose(); return strResult; }
涉及到的實體:
public class WXApi { public string access_token { set; get; } }
以上是“使用C#微信接口實現推送模板消息功能的案例”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。