您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“微信開發(fā)之微信發(fā)送消息的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“微信開發(fā)之微信發(fā)送消息的示例分析”這篇文章吧。
1,首先,獲取開發(fā)者測試賬號(申請),會根據(jù)當(dāng)前掃碼提供的賬號生成測試賬號: 鏈接地址:http://mp.weixin.qq.com/wiki/home/index.html
這時候可以獲取到測試用的appid和appsecrept,然后調(diào)用獲取接口調(diào)用憑證 接口獲取access_token;
2,下面說信息發(fā)送,模擬了單用戶信息發(fā)送和多用戶消息批量發(fā)送
?。?)基礎(chǔ)方法,http方法
/// <summary> /// http get/post 公用方法 /// </summary> /// <param name="requestUrl">請求鏈接</param> /// <param name="requestJsonParams">請求參數(shù)值(如果是get方式此處為“”值,默認(rèn)為 "")</param> /// <param name="requestMethod">請求方式 post or get</param> /// <returns></returns> public static string Request(this string requestUrl, string requestMethod, string requestJsonParams = "") { string returnText = ""; StreamReader streamReader = null; HttpWebRequest request = null; HttpWebResponse response = null; Encoding encoding = Encoding.UTF8; request = (HttpWebRequest)WebRequest.Create(requestUrl); request.Method = requestMethod; if (!string.IsNullOrEmpty(requestJsonParams) && requestMethod.ToLower() == "post") { byte[] buffer = encoding.GetBytes(requestJsonParams); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); } try { response = (HttpWebResponse)request.GetResponse(); using (streamReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312")))//utf-8 { returnText = streamReader.ReadToEnd(); } } catch (Exception ex) { returnText = ex.Message; } return returnText; }
(2)模擬發(fā)送:
/// <summary> /// 發(fā)送微信信息(單用戶發(fā)送) /// </summary> /// <param name="access_token">授權(quán)碼(微信token)</param> /// <param name="messageInfo">發(fā)送信息模型</param> /// <returns></returns> public static string SendSingleMessage(WeChatParamEntity messageInfo, string access_token) { messageInfo.MsgType = string.IsNullOrEmpty(messageInfo.MsgType) ? "text" : messageInfo.MsgType; string jsonDataParams = messageInfo == null ? "" : JsonConvert.SerializeObject(new { touser = messageInfo.ToUser, msgtype = messageInfo.MsgType, text = new { content = messageInfo.Text } }); string requestUrl = string.Format(Consts.URL_POSTSINGLETEXTMESSAGE, access_token); return requestUrl.Request("POST", jsonDataParams); } /// <summary> /// 發(fā)送微信信息(多用戶批量發(fā)送) /// </summary> /// <param name="access_token">授權(quán)碼(微信token)</param> /// <param name="messageInfo">發(fā)送信息模型</param> /// <returns></returns> public static string SendMessages(WeChatParamsEntity messageInfo, string access_token) { messageInfo.MsgType = string.IsNullOrEmpty(messageInfo.MsgType) ? "text" : messageInfo.MsgType; string jsonDataParams = messageInfo == null ? "" : JsonConvert.SerializeObject(new { touser = messageInfo.ToUser, msgtype = messageInfo.MsgType, text = new { content = messageInfo.Text } }); string requestUrl = string.Format(Consts.URL_POSTTEXTMESSAGES, access_token); return requestUrl.Request("POST", jsonDataParams); }
?。?)兩個參數(shù) 模型:
/// <summary> /// 微信 發(fā)送信息 參數(shù)實體模型 /// </summary> public class WeChatParamEntity { /// <summary> /// 普通用戶openid /// </summary> public string ToUser { get; set; } /// <summary> /// 傳輸?shù)奈募愋停╰ext,image, and so on) /// </summary> public string MsgType { get; set; } = "text"; /// <summary> /// 傳輸文本內(nèi)容 /// </summary> public string Text { get; set; } } /// <summary> /// 微信 發(fā)送信息 參數(shù)實體模型 /// </summary> public class WeChatParamsEntity { /// <summary> /// 普通用戶openid /// </summary> public string[] ToUser { get; set; } /// <summary> /// 傳輸?shù)奈募愋停╰ext,image, and so on) /// </summary> public string MsgType { get; set; } = "text"; /// <summary> /// 傳輸文本內(nèi)容 /// </summary> public string Text { get; set; } }
?。?)web.config中的鏈接
<!--微信接口--> <add key="appid" value="wx123456789021"/> <add key="appSecret" value="adasdsadasdasdsadasdsaqweqw"/> <add key="getAccessTokenUrl" value="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"/> <!--單用戶信息發(fā)送--> <add key="postSingleTextMessageUrl" value="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}"/> <!--多用戶批量發(fā)送--> <add key="postTextMessagesUrl" value="https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token={0}"/> <!--\微信接口-->
3,測試使用涉及到 touser的這個參數(shù),這個是需要發(fā)送的對象的 openID,這個很簡單,在開發(fā)者文檔(也就是上面的步驟二中,)獲取
appid 和appsecrept的時候,當(dāng)前這個頁面下面有一個二維碼,找?guī)讉€人用微信掃掃就可以自動獲取openID ,這時候?qū)?shù)帶入腳本模擬
post即可
另外需要注意:文檔中提示的 json 參數(shù)格式
注意三:token有效時間為7200,倆小時,需要判斷當(dāng)前發(fā)送信息用戶的token有效性,同時每天最大可請求次數(shù)為2000.
獲取token :
#region 獲取token,并驗證token過期時間 public static string GetAccessToken(string appid, string appSecret) { string token = ""; string requestUrl = string.Format(ConfigBLL.URL_GETACCESSTOKEN, appid, appSecret); string requestResult = WebAPITransfer.Request(requestUrl, "GET", ""); CommonBLL.DebugLog(requestResult, "AccessToken-token-Params"); string[] strArray = requestResult.Split(','); token = strArray[0].Split(':')[1].Replace("\"", ""); return token; } #endregion
以上是“微信開發(fā)之微信發(fā)送消息的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。