.NET 往微信公众号 发送消息

public string gettocken()

        {

          string url = @"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=xxx&corpsecret=rxxY";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "GET";

            //request.ContentType = "application/x-www-form-urlencoded";

            //request.ContentType = "application/json"; 

            request.ReadWriteTimeout = 30 * 1000;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();


            StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            var retString = myStreamReader.ReadToEnd();

          // MessageBox.Show(retString);

            JObject jo = JObject.Parse(retString);

            string ss=jo["access_token"].ToString();

            myStreamReader.Close();

            return ss;

        }

        public void Post(string mes)

        {

            StringWriter sw = new StringWriter();

            JsonWriter jwriter = new JsonTextWriter(sw);

            jwriter.WriteStartObject();

            jwriter.WritePropertyName("touser");

            jwriter.WriteValue("user1|user2");

            jwriter.WritePropertyName("toparty");

            jwriter.WriteValue("");

            jwriter.WritePropertyName("totag");

            jwriter.WriteValue("");

            jwriter.WritePropertyName("msgtype");

            jwriter.WriteValue("text");

            jwriter.WritePropertyName("agentid");

            jwriter.WriteValue("1000004");

            jwriter.WriteRaw(",\"text\":{\"content\":\""+mes+"\"}");

            jwriter.WriteEndObject();

            jwriter.Flush();

            string postDataStr = sw.GetStringBuilder().ToString();


          //  ConverttoISO88591(postDataStr);

          // MessageBox.Show(postDataStr);

            ///获取token

            string accesstoken = gettocken();

            ///发送模版消息

            string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", accesstoken);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "POST";

          // request.ContentType = "application/json";

              request.ContentType = "application/x-www-form-urlencoded";

            request.ContentLength = postDataStr.Length;


            StreamWriter writer = new StreamWriter(request.GetRequestStream(),Encoding.ASCII);

            writer.Write(postDataStr);

            writer.Flush();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string encoding = response.ContentEncoding;

            if (encoding == null || encoding.Length < 1)

            {

                encoding = "UTF-8"; //默认编码 

            }

            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));

            string retString = reader.ReadToEnd();

        //    MessageBox.Show(retString);


        }

你可能感兴趣的:(.NET 往微信公众号 发送消息)