您好,登錄后才能下訂單哦!
這篇文章主要介紹微信開發如何實現回復用戶消息功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
示例
/// </summary> /// <param name="UserID">要發送的人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("回復失敗!"); Como.LogUtil.WriteException("回復失敗:SendMessage:" + 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(); //發送請求并獲取相應回應數據 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才開始向目標網頁發送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 //返回頁面請求信息 結果
以上為基礎發送關鍵代碼:
以下為:成功后的實例圖:
以上是“微信開發如何實現回復用戶消息功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。