搜尋
首頁微信小程式微信開發微信開發回覆用戶訊息的程式碼實例分享

前陣子發展了公司的微信

 這裡做個知識總結分享下經驗,望一起學習。 。 。 。 。

 感覺後面寫個系列的最好了 。 。 。 。

 企業需求:

     給予指定企業使用者傳送訊息:如考勤資訊、企業通知、個人資訊推送等等, 

/// </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 += &#39;"&#39; + UserID + &#39;"&#39;;
            Text += "," + &#39;"&#39; + @"msgtype"": ""text"", 
           ""agentid"": ""5"", 
            ""text"": {
           ""content"":";
            Text += &#39;"&#39; + StrMessg + &#39;"&#39;;
            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  //回頁請求訊息 結果

 以上傳送關鍵程式碼:

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

微信開發回覆用戶訊息的程式碼實例分享

以上是微信開發回覆用戶訊息的程式碼實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)