微信開發(fā)如何實現(xiàn)回復(fù)用戶消息功能-創(chuàng)新互聯(lián)

這篇文章主要介紹微信開發(fā)如何實現(xiàn)回復(fù)用戶消息功能,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比龍城網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式龍城網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋龍城地區(qū)。費(fèi)用合理售后完善,十余年實體公司更值得信賴。

示例


/// </summary>
        /// <param name="UserID">要發(fā)送的人ID</param>
        /// <param name="StrMessg">消息</param>
        private void SendMessage(string UserID, string StrMessg)
        {
            Como.LogUtil.WriteLog("回復(fù)用戶" + 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("回復(fù)成功!");
            }
            else
            {
                Como.LogUtil.WriteLog("回復(fù)失??!");
                Como.LogUtil.WriteException("回復(fù)失?。篠endMessage:" + strResult); 
            }
        }
/// <summary>
        /// 根據(jù)請求地址 和數(shù)據(jù)返回請求結(jié)果
        /// </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);

            // 準(zhǔn)備請求...

            try
            {

                // 設(shè)置參數(shù)

                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ā)送請求并獲取相應(yīng)回應(yīng)數(shù)據(jù)

                response = request.GetResponse() as HttpWebResponse;

                //直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁發(fā)送Post請求

                instream = response.GetResponseStream();

                sr = new StreamReader(instream, encoding);

                //返回結(jié)果網(wǎng)頁(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;

            }

        }

參數(shù)引用詳解:

1.Como.GetAccessToken.GetAccess_token()   //獲取 Access_token

2.GetPage  //返回頁面請求信息 結(jié)果

以上為基礎(chǔ)發(fā)送關(guān)鍵代碼:

以下為:成功后的實例圖:

微信開發(fā)如何實現(xiàn)回復(fù)用戶消息功能

以上是“微信開發(fā)如何實現(xiàn)回復(fù)用戶消息功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:微信開發(fā)如何實現(xiàn)回復(fù)用戶消息功能-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://muchs.cn/article16/coeegg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)頁設(shè)計公司、電子商務(wù)、外貿(mào)網(wǎng)站建設(shè)、軟件開發(fā)、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(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)

成都定制網(wǎng)站建設(shè)