asp.net 實現微信公眾平臺的主動推送資訊
通過學習借鑑朋友的實現方法進行整理(微信公眾帳號主動傳送訊息給使用者,asp.net版本)。
/// <summary>
/// MD5 32位加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static string GetMd5Str32(string str)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
char[] temp = str.ToCharArray();
byte[] buf = new byte[temp.Length];
for (int i = 0; i < temp.Length; i++)
{
buf[i] = (byte)temp[i];
}
byte[] data = md5Hasher.ComputeHash(buf);
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
public static bool ExecLogin()
{
bool result = false;
string password = GetMd5Str32(strMPPassword).ToUpper(); //注意轉換為大寫
string padata = "username=" + System.Web.HttpUtility.UrlEncode(strMPAccount) + "&pwd=" + password + "&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.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來序列化
SunnyInfo.Web.Class.WeiXinRetInfo retinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<SunnyInfo.Web.Class.WeiXinRetInfo>(text2);
string token = string.Empty;
if (retinfo.ErrMsg.Length > 0)
{
token = retinfo.ErrMsg.Split(new char[] { '&' })[2].Split(new char[] { '=' })[1].ToString();//取得令牌
LoginInfo.LoginCookie = cc;
LoginInfo.CreateDate = DateTime.Now;
LoginInfo.Token = token;
result = true;
}
}
catch (Exception ex)
{
Company.PubClass.WriteLog("Erro[" + DateTime.Now.ToString() + "]" + ex.StackTrace);
}
return result;
}
public static class LoginInfo
{
/// <summary>
/// 登入後得到的令牌
/// </summary>
public static string Token { get; set; }
/// <summary>
/// 登入後得到的cookie
/// </summary>
public static CookieContainer LoginCookie { get; set; }
/// <summary>
/// 建立時間
/// </summary>
public static DateTime CreateDate { get; set; }
}
//傳送資訊
public static bool SendMessage(string Message, string fakeid)
{
bool result = false;
CookieContainer cookie = null;
string token = null;
//此處的作用是判斷Cookie是否過期如果過期就重新獲取,獲取cookie的方法本人在.net 實現微信公眾平臺的主動推送資訊中有原始碼。大家可以去看一下。這裡就不再粘原始碼了。
if (null == Class.WeiXinLogin.LoginInfo.LoginCookie || Class.WeiXinLogin.LoginInfo.CreateDate.AddMinutes(Convert.ToInt32(Class.WeiXinLogin.strLoingMinutes)) < DateTime.Now)
{
Class.WeiXinLogin.ExecLogin();
}
cookie = Class.WeiXinLogin.LoginInfo.LoginCookie;//取得cookie
token = Class.WeiXinLogin.LoginInfo.Token;//取得token
string strMsg = System.Web.HttpUtility.UrlEncode(Message);
string padate = "type=1&content=" + strMsg + "&error=false&tofakeid=" + fakeid + "&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 = cookie; //登入時得到的快取
webRequest2.Referer = "https://mp.weixin.qq.com/cgi-bin/singlemsgpage?token=" + token + "&fromfakeid=" + fakeid + "&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();
if (text2.Contains("ok"))
{
result = true;
}
return result;
}
相關文章
- 微信公眾平臺介面APIAPI
- 微信公眾平臺SDK for node
- 記一次接收微信公眾平臺推送訊息的例項
- 微信公眾平臺JSSDK分享介面開發(PHP實現)JSPHP
- 微信公眾平臺/擴充套件套件
- 微信公眾平臺基礎框架框架
- 第27章微信公眾平臺
- 自制公眾平臺Web Api(微信)WebAPI
- 公眾號傳送模板資訊java實現(主動傳送)Java
- 微信公眾平臺開發入門
- 微信公眾平臺通用介面API指南API
- Senparc.Weixin.MP SDK 微信公眾平臺開發教程(三):微信公眾平臺開發驗證
- java實現 微信公眾號推送訊息 ,cv 就可執行!!!Java
- 基於 PHP 的微信公眾平臺開發PHP
- 使用微信公眾平臺傳送報警資訊(Python版)薦Python
- 2.PHP微信公眾平臺開發(二) 公眾平臺示例程式碼分PHP
- PHP微信公眾平臺開發視訊PHP
- 微信公眾平臺軟文采集專案
- 微信公眾號定時群發平臺
- 微信公眾平臺開發(十一) 功能整合
- 微信公眾平臺完整開發教程 by ZTalk
- 微信阻公眾平臺成營銷工具
- 淺談微信公眾平臺運用的場景
- 微信公眾平臺開發之店鋪類
- 個人微信公眾平臺註冊前須知
- 最新微信公眾平臺js sdk整合PHP版JSPHP
- warehouse的微信公眾平臺,歡迎訂閱關注!
- Laravel 配置微信公眾平臺驗證 token 失敗Laravel
- 微信公眾平臺搭建與開發揭祕.pdf
- 微信公眾平臺開發(77) 圖片下載
- 微信公眾平臺iPhone版正式開啟內測iPhone
- 微信公眾平臺開發(九) 資料庫操作資料庫
- 微信公眾號開發推送事件排重事件
- 微信公眾平臺開發(81) 2014新年微信賀卡
- 微信公眾平臺運營失敗的六脈診斷
- Java微信公眾號推送模版訊息的方法示例Java
- ThinkJS 接入微信公眾平臺 2 —— 獲取 AccessTokenJS
- 9.微信公眾平臺開發 - 資料庫操作資料庫