.net 實現微信公眾平台的主動推送信息


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);  

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM