.net 实现微信公众平台的主动推送信息

本文介绍了一种使用C#实现的微信公众号登录及发送消息的方法。通过构造特定的HTTP请求,实现了公众号的自动登录,并能向指定用户发送文本消息。文章详细展示了如何通过POST请求传递登录所需的参数,并获取必要的令牌;同时介绍了如何利用该令牌构造消息发送请求。

protected void Page_Load(object sender, EventArgs e)
{          

    string padata = "username=用户名&pwd=md5的密码&imgcode=&f=json";//用于登录要发送的数据
            string url = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ";//请求登录的URL
            try
            {
                CookieContainer cc = new CookieContainer();//接收缓存
                byte[] byteArray = Encoding.UTF8.GetBytes(padata); // 转化
                HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);
                webRequest2.CookieContainer = cc;
                webRequest2.Method = "POST";
                webRequest2.ContentType = "application/x-www-form-urlencoded";

      webRequest2.Referer = "https://mp.weixin.qq.com/";
                webRequest2.ContentLength = byteArray.Length;
                Stream newStream = webRequest2.GetRequestStream();
                    // Send the data.
                newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
                newStream.Close();
                HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
                StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default);
                string text2 = sr2.ReadToEnd();            

                //此处用到了newtonsoft来序列化。大家可以不用这个,也可以自己手动写代码
                RetInfo retinfo=Newtonsoft.Json.JsonConvert.DeserializeObject<RetInfo>(text2);
                string token = retinfo.ErrMsg.Split(new char[] { '&' })[2].Split(new char[]{'='})[1].ToString();//取得令牌
                SendMessage(cc,token);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.StackTrace);
            }

}

public void SendMessage(CookieContainer cc, string token)        

{                       

   string padate = "type=1&content=要发送的内容(注意要先进行Url编码)&error=false&tofakeid=22108785&token=" + token + "&ajax=1";  

       string url = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN";

        byte[] byteArray = Encoding.UTF8.GetBytes(padate); // 转化

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

        webRequest2.CookieContainer = cc; //登录时得到的缓存

        webRequest2.Referer = "https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token=" + token + "&fromfakeid=22108785&msgid=&source=&count=20&t=wxm-singlechat&lang=zh_CN";

       webRequest2.Method = "POST";

       webRequest2.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";

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

         webRequest2.ContentLength = byteArray.Length; 

         Stream newStream = webRequest2.GetRequestStream();

        // Send the data.            

     newStream.Write(byteArray, 0, byteArray.Length);    //写入参数    

     newStream.Close();

     HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();

     StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);

    string text2 = sr2.ReadToEnd();  

    Response.Write(text2);  

}

 

 

转载于:https://www.cnblogs.com/ytmom/archive/2013/04/23/3038808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值