Beispiel #1
0
        /// <summary>
        /// 给指定的微信账号推送消息发送文本类型的客服消息
        /// </summary>
        /// <param name="msg">消息内容</param>
        /// <returns></returns>
        public static Boolean SendCustomMsg(String msg, String openid)
        {
            if (msg == null)
            {
                return(false);
            }

            var a = new { touser = openid, msgtype = "text", text = new { content = msg } };

            // 创建HTTP请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Common.BaseAPIUrl + "cgi-bin/message/custom/send?access_token=" + Config.AccessToken);

            request.Method = "POST";

            // 写参数到请求流
            String jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(a);

            byte[] b = System.Text.Encoding.UTF8.GetBytes(jsonStr);
            request.ContentLength = b.Length;
            System.IO.Stream stream = request.GetRequestStream();
            stream.Write(b, 0, b.Length);
            stream.Close();

            // 获取返回
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                System.IO.Stream       s = response.GetResponseStream();
                System.IO.StreamReader r = new System.IO.StreamReader(s, System.Text.Encoding.UTF8);
                String responseStr       = r.ReadToEnd();
                AppDebugError.Append(new AppDebugError("TextMessage", "SendCustomMsg", "发送客服消息返回:" + responseStr));
                response.Close();
                return(true);
            }
        }
Beispiel #2
0
        public static WebAccessToken GetWebAccessToken(String code)
        {
            String          requestURL = String.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", Config.AppID, Config.AppSecret, code);
            HttpWebRequest  request    = (HttpWebRequest)WebRequest.Create(requestURL);
            HttpWebResponse response   = (HttpWebResponse)request.GetResponse();

            System.IO.Stream       stream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.UTF8);
            String responseText           = reader.ReadToEnd();

            // 返回信息
            AppDebugError.Append(new AppDebugError("Authorize", "WebAccessToken", "获取授权返回内容:" + responseText));

            var            a      = new { access_token = "", expires_in = 0, refresh_token = "", openid = "", scope = "" };
            var            b      = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(responseText, a);
            WebAccessToken result = new WebAccessToken()
            {
                AccessToken  = b.access_token,
                ExpiresIn    = b.expires_in,
                OpenID       = b.openid,
                RefreshToken = b.refresh_token,
                Scope        = b.scope
            };

            response.Close();
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// 发送模板消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public static Boolean Send(TemplateMessage msg, ref String errMsg)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg), "参数是必须的!");
            }


            String objJson = "{";

            objJson += "\"touser\":\"" + msg.ToUser + "\",";
            objJson += "\"template_id\":\"" + msg.TemplateID + "\",";
            objJson += "\"url\":\"" + msg.Url + "\",";
            if (!String.IsNullOrEmpty(msg.MiniProgram) && msg.MiniProgram.Length > 1)
            {
                objJson += "\"miniprogram\":" + msg.MiniProgram + ",";
            }
            objJson += "\"data\":" + msg.Data;
            objJson += "}";

            AppDebugError.Append(new AppDebugError(nameof(TemplateMessage), nameof(Send), "发送模板消息内容:" + objJson));

            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Common.BaseAPIUrl + "cgi-bin/message/template/send?access_token=" + Config.AccessToken);
            request.Method = "POST";

            Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(objJson);
            request.ContentLength = bytes.Length;

            System.IO.Stream stream = request.GetRequestStream();
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();

            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.StreamReader     reader   = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
            String responseStr = reader.ReadToEnd();

            reader.Close();
            response.Close();

            if (responseStr.Contains("errcode"))
            {
                var r = new { errcode = 0, errmsg = "", msgid = "" };
                var m = JsonConvert.DeserializeAnonymousType(responseStr, r);
                if (m.errcode == 0)
                {
                    return(true);
                }
                else
                {
                    errMsg = m.errcode + ":" + m.errmsg;
                    AppDebugError.Append(new AppDebugError(nameof(TemplateMessage), nameof(Send), "发送模板消息返回错误:" + errMsg));
                    return(false);
                }
            }
            else
            {
                errMsg = "服务器返回的字符串为:" + responseStr;
                AppDebugError.Append(new AppDebugError(nameof(TemplateMessage), nameof(Send), "发送模板消息返回:" + errMsg));
                return(false);
            }
        }