您好,登錄后才能下訂單哦!
這篇文章主要介紹微信開發(fā)如何實現回復用戶消息功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
示例
/// </summary> /// <param name="UserID">要發(fā)送的人ID</param> /// <param name="StrMessg">消息</param> private void SendMessage(string UserID, string StrMessg) { Como.LogUtil.WriteLog("回復用戶" + UserID + "消息"); string Access_Token = Como.GetAccessToken.GetAccess_token(); if (Access_Token == "") Como.LogUtil.WriteException("SendMessage 未能成功加載Access_Token"); string Text = @"{ ""touser"":"; Text += '"' + UserID + '"'; Text += "," + '"' + @"msgtype"": ""text"", ""agentid"": ""5"", ""text"": { ""content"":"; Text += '"' + StrMessg + '"'; Text += @"}, ""safe"": ""0"" }"; ; string url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", Access_Token); string strResult = Como.Tools.GetPage(url, Text); JArray ja = (JArray)JsonConvert.DeserializeObject("[" + strResult + "]"); string Error = ""; try { if (strResult.Contains("errcode")) { Error = ja[0]["errcode"].ToString(); } } catch (Exception ex) { Como.LogUtil.WriteException("獲取strResult,未獲取到錯誤信息" + ex.Message.ToString()); } string errcode = ja[0]["errcode"].ToString(); string errmsg = ja[0]["errmsg"].ToString(); if (errcode == "0" && errmsg == "ok") { Como.LogUtil.WriteLog("回復成功!"); } else { Como.LogUtil.WriteLog("回復失?。?quot;); Como.LogUtil.WriteException("回復失?。篠endMessage:" + strResult); } }
/// <summary> /// 根據請求地址 和數據返回請求結果 /// </summary> /// <param name="posturl"></param> /// <param name="postData"></param> /// <returns></returns> public static string GetPage(string posturl, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 準備請求... try { // 設置參數 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //發(fā)送請求并獲取相應回應數據 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才開始向目標網頁發(fā)送Post請求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回結果網頁(html)代碼 string content = sr.ReadToEnd(); string err = string.Empty; return content; } catch (Exception ex) { string err = ex.Message; Como.LogUtil.WriteException("Tools GetPage 異常為:"+err.ToString()); return string.Empty; } }
參數引用詳解:
1.Como.GetAccessToken.GetAccess_token() //獲取 Access_token
2.GetPage //返回頁面請求信息 結果
以上為基礎發(fā)送關鍵代碼:
以下為:成功后的實例圖:
以上是“微信開發(fā)如何實現回復用戶消息功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。