C#實(shí)現(xiàn)微信公眾號(hào)群發(fā)消息(解決一天只能發(fā)一次的限制)實(shí)例分享
總體思路:
1.首先必須要在微信公眾平臺(tái)上申請(qǐng)一個(gè)公眾號(hào)。
2.然后進(jìn)行模擬登陸。(由于我對(duì)http傳輸原理和編程不是特別懂,在模擬登陸的地方,不是特別清楚,希望有大神指教)
3.模擬登陸后會(huì)獲得一個(gè)token(令牌)和cookie。
4.因?yàn)槟M登陸后相當(dāng)于就進(jìn)入了微信公眾平臺(tái),在這個(gè)里面就可以抓取到需要的數(shù)據(jù),如公眾好友的昵稱(chēng),fakeId。其中的fakeid非常重要,因?yàn)閭鬏敂?shù)據(jù)必須要知道對(duì)方的fakeid。
5.知道對(duì)方的fakeid就可以進(jìn)行數(shù)據(jù)的發(fā)送了。
這里是整個(gè)項(xiàng)目的源碼下載:http://http://xiazai.jb51.net/201309/yuanma/webchat.rar
不過(guò)里面還有一些小問(wèn)題,希望有人繼續(xù)修改和討論!也有人說(shuō)這樣會(huì)被封號(hào),所以請(qǐng)謹(jǐn)慎操作
講一下我項(xiàng)目里面的主要內(nèi)容
1.WeiXinLogin.cs類(lèi)是用來(lái)執(zhí)行登陸功能的
//對(duì)密碼進(jìn)行MD5加密
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();
}
//執(zhí)行登陸操作
public static bool ExecLogin(string name,string pass)
{
bool result = false;
string password = GetMd5Str32(pass).ToUpper();
string padata = "username=" + name + "&pwd=" + password + "&imgcode=&f=json";
string url = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN ";//請(qǐng)求登錄的URL
try
{
CookieContainer cc = new CookieContainer();//接收緩存
byte[] byteArray = Encoding.UTF8.GetBytes(padata); // 轉(zhuǎn)化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url); //新建一個(gè)WebRequest對(duì)象用來(lái)請(qǐng)求或者響應(yīng)url
webRequest2.CookieContainer = cc; //保存cookie
webRequest2.Method = "POST"; //請(qǐng)求方式是POST
webRequest2.ContentType = "application/x-www-form-urlencoded"; //請(qǐng)求的內(nèi)容格式為application/x-www-form-urlencoded
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream(); //返回用于將數(shù)據(jù)寫(xiě)入 Internet 資源的 Stream。
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //寫(xiě)入?yún)?shù)
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
//此處用到了newtonsoft來(lái)序列化
WeiXinRetInfo retinfo = Newtonsoft.Json.JsonConvert.DeserializeObject<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)
{
throw new Exception(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>
/// 創(chuàng)建時(shí)間
/// </summary>
public static DateTime CreateDate { get; set; }
}
2.在WeiXin.cs類(lèi)中實(shí)現(xiàn)發(fā)送數(shù)據(jù)
public static bool SendMessage(string Message, string fakeid)
{
bool result = false;
CookieContainer cookie = null;
string token = null;
cookie = WeiXinLogin.LoginInfo.LoginCookie;//取得cookie
token = WeiXinLogin.LoginInfo.Token;//取得token
string strMsg = System.Web.HttpUtility.UrlEncode(Message); //對(duì)傳遞過(guò)來(lái)的信息進(jìn)行url編碼
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); // 轉(zhuǎn)化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);
webRequest2.CookieContainer = cookie; //登錄時(shí)得到的緩存
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); //寫(xiě)入?yún)?shù)
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;
}
3.SendMessage.aspx.cs中主要實(shí)現(xiàn)獲取fakeid
public static ArrayList SubscribeMP()
{
try
{
CookieContainer cookie = null;
string token = null;
cookie = WeiXinLogin.LoginInfo.LoginCookie;//取得cookie
token = WeiXinLogin.LoginInfo.Token;//取得token
/*獲取用戶信息的url,這里有幾個(gè)參數(shù)給大家講一下,1.token此參數(shù)為上面的token 2.pagesize此參數(shù)為每一頁(yè)顯示的記錄條數(shù)
3.pageid為當(dāng)前的頁(yè)數(shù),4.groupid為微信公眾平臺(tái)的用戶分組的組id,當(dāng)然這也是我的猜想不一定正確*/
string Url = "https://mp.weixin.qq.com/cgi-bin/contactmanagepage?t=wxm-friend&token=" + token + "&lang=zh_CN&pagesize=10&pageidx=0&type=0&groupid=0";
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(Url);
webRequest2.CookieContainer = cookie;
webRequest2.ContentType = "text/html; charset=UTF-8";
webRequest2.Method = "GET";
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";
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
MatchCollection mc;
//由于此方法獲取過(guò)來(lái)的信息是一個(gè)html網(wǎng)頁(yè)所以此處使用了正則表達(dá)式,注意:(此正則表達(dá)式只是獲取了fakeid的信息如果想獲得一些其他的信息修改此處的正則表達(dá)式就可以了。)
Regex r = new Regex("\"fakeId\"\\s\\:\\s\"\\d+\""); //定義一個(gè)Regex對(duì)象實(shí)例
mc = r.Matches(text2);
Int32 friendSum = mc.Count; //好友總數(shù)
string fackID ="";
ArrayList fackID1 = new ArrayList();
for (int i = 0; i < friendSum; i++)
{
fackID = mc[i].Value.Split(new char[] { ':' })[1];
fackID = fackID.Replace("\"", "").Trim();
fackID1.Add(fackID);
}
return fackID1;
}
catch (Exception ex)
{
throw new Exception(ex.StackTrace);
}
}
相關(guān)文章
C#使用FolderBrowserDialog類(lèi)實(shí)現(xiàn)選擇打開(kāi)文件夾方法詳解
這篇文章主要介紹了C#選擇文件夾/打開(kāi)文件夾/瀏覽文件夾等代碼方法,大家參考使用2013-11-11C#?在PDF中添加墨跡注釋Ink?Annotation的步驟詳解
PDF中的墨跡注釋表現(xiàn)為徒手涂鴉式的形狀,該類(lèi)型的注釋?zhuān)扇我庵付ㄐ螤铐旤c(diǎn)的位置及個(gè)數(shù),通過(guò)指定的頂點(diǎn),程序?qū)⑦B接各點(diǎn)繪制成平滑的曲線,下面通過(guò)C#程序代碼介紹下在pdf中添加注釋的步驟,感興趣的朋友一起看看吧2022-02-02Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(2)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲的第二部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C#使用偽隨機(jī)數(shù)實(shí)現(xiàn)加密用戶密碼的方法
這篇文章主要介紹了C#使用偽隨機(jī)數(shù)實(shí)現(xiàn)加密用戶密碼的方法,對(duì)于開(kāi)發(fā)C#會(huì)員系統(tǒng)或者程序安全問(wèn)題都有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-07-07C#中DataGridView導(dǎo)出Excel的兩種方法
這篇文章主要介紹了C#中DataGridView導(dǎo)出Excel的兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01C#制作網(wǎng)站掛機(jī)程序的實(shí)現(xiàn)示例
本文主要介紹了C#制作網(wǎng)站掛機(jī)程序,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10C#3.0使用EventLog類(lèi)寫(xiě)Windows事件日志的方法
這篇文章主要介紹了C#3.0使用EventLog類(lèi)寫(xiě)Windows事件日志的方法,以簡(jiǎn)單實(shí)例形式分析了C#寫(xiě)windows事件日志的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#中委托和事件在觀察者模式中的應(yīng)用實(shí)例
這篇文章主要介紹了C#中委托和事件在觀察者模式中的應(yīng)用,需要的朋友可以參考下2014-08-08