c#给微信公众号发送通知

 //给微信公众号发送通知
        [HttpGet]
        public string SendWeChatMsg()
        {
            string OpenID = "";
            //Appid
            string appid = "";
            //secret
            string secret = "";
            if (OpenID != "")
            {
                string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret + "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.ContentType = "textml;charset=UTF-8";
                string jsonData = "";
                using (HttpWebResponse response1 = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader sr = new StreamReader(response1.GetResponseStream(), Encoding.UTF8))
                    {
                        jsonData = sr.ReadToEnd();
                        sr.Close();
                    }
                    response1.Close();
                }
                if (jsonData != "")
                {
                    string jsonString = jsonData;
                    JObject json = JObject.Parse(jsonString);
                    string access_token = json["access_token"].ToString();

                    string str = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;

                    //json参数,传入对应的值                                                                           
                    string jsonParam = "{\"touser\": \"" + OpenID + "\",\"template_id\": \"\",\"data\": {\"cols1\": { \"value\": \"\"}," +
                        "\"cols2\": { \"value\": \"\"}," +
                          "\"cols3\": { \"value\": \"\"}," +
                            "\"cols4\": { \"value\": \"\"}}}";

                    HttpWebRequest requests = (HttpWebRequest)WebRequest.Create(str);
                    requests.Method = "POST";
                    requests.Timeout = 20000;
                    requests.ContentType = "application/json;charset=UTF-8";
                    byte[] byteData = Encoding.UTF8.GetBytes(jsonParam);
                    int length = byteData.Length;
                    requests.ContentLength = length;

                    using (Stream writer = requests.GetRequestStream())
                    {
                        writer.Write(byteData, 0, length);
                        writer.Close();
                    }

                    string jsonStrings = string.Empty;
                    using (HttpWebResponse responses = (HttpWebResponse)requests.GetResponse())
                    {
                        using (Stream streams = responses.GetResponseStream())
                        {
                            using (StreamReader readers = new StreamReader(streams, System.Text.Encoding.UTF8))
                            {
                                jsonStrings = readers.ReadToEnd();
                                responses.Close();
                                streams.Close();
                                readers.Close();
                            }
                        }
                    }
                }
            }
            return "";
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值