微信公衆號的開發之 向用戶發送消息(三)

  /// <summary>
        /// 向用戶發送消息
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="contentText"></param>
        private void send(string openid, string contentText)
        {

            Hashtable where = new Hashtable();
            where.Add("access_token", access_token());

            string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";

            //定義JSON數據
            JObject content = new JObject();
            content.Add("content", contentText);

            JObject j = new JObject();
            j.Add("touser", openid);
            j.Add("text", content);
            j.Add("msgtype", "text");

            string result = Util.HttpRequestPostBody(url, where, j.ToString());
            throwErrmsg(result);
        }

        /// <summary>
        /// 拋出微信異常結果
        /// </summary>
        /// <param name="result"></param>
        private void throwErrmsg(string result)
        {
            Hashtable hr = JsonConvert.DeserializeObject<Hashtable>(result);
            if (hr["errcode"].ToString().ToInt32() != 0)
            {
                throw new Exception(hr["errmsg"].ToString());
            }
        }


這裏調用到的函數可以查看 微信公衆號的開發之 自定義菜單(二) 這篇日誌



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章