微信開發 回覆用戶消息 .net C#

前段時間開發了公司的微信html

 這裏作個知識總結分享下經驗,望一塊兒學習。。。。。api

 感受後面寫個系列的最好了 。。。。微信

 企業需求:cookie

     給指定企業用戶發送消息:如考勤信息、 企業通知、我的信息推送等等, app

  

   /// </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;

            }

        }

 

  參數引用詳解:post

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

       2.GetPage  //返回頁面請求信息 結果url

 

 以上爲基礎發送關鍵代碼:spa

 如下爲:成功後的實例圖:code

  有不懂的地方你們能夠留言:

        我的原創,請勿抄襲!

相關文章
相關標籤/搜索