博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net 实现微信公众平台的主动推送信息
阅读量:4492 次
发布时间:2019-06-08

本文共 2265 字,大约阅读时间需要 7 分钟。

 

 

protected void Page_Load(object sender, EventArgs e) {          

    string padata = "username=用户名&pwd=md5的密码&imgcode=&f=json";             string 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.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();                               RetInfo retinfo=Newtonsoft.Json.JsonConvert.DeserializeObject<RetInfo>(text2);//此处用到了newtonsoft来序列化。大家可以不用这个,也可以自己手动写代码                 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 = "";

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

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

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

        webRequest2.Referer = "=" + 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/summay/archive/2013/04/23/3039124.html

你可能感兴趣的文章
数据解析01-15
查看>>
linux 安装mysql数据库——yum安装法
查看>>
Several ports (8005, 80, 8009) required by Tomcat v6.0 Server at localhost are already in use
查看>>
事件监听器
查看>>
设计模式之单例设计模式
查看>>
异常的基本概念
查看>>
vue 在发送axios请求时数据渲染问题
查看>>
动态链接库dll
查看>>
2018 Multi-University Training Contest 3 - HDU Contest
查看>>
组合数取模(转载)
查看>>
9.2NOIP模拟题
查看>>
整合SpringDataJpa
查看>>
vue过渡
查看>>
tcpreplay 博客目录
查看>>
oracle11g忘记sys密码
查看>>
文件各种上传,离不开的表单
查看>>
mysql查询插入优化
查看>>
hadoop备战:yarn框架的搭建(mapreduce2)
查看>>
微信公众号开发模式开启总结
查看>>
pygame-KidsCanCode系列jumpy-part2-加速度与摩擦力
查看>>