C#实现微信公众号群发消息(解决一天只能发一次的限制)实例分享
2023-06-13 09:15:05 时间
总体思路:
1.首先必须要在微信公众平台上申请一个公众号。
2.然后进行模拟登陆。(由于我对http传输原理和编程不是特别懂,在模拟登陆的地方,不是特别清楚,希望有大神指教)
3.模拟登陆后会获得一个token(令牌)和cookie。
4.因为模拟登陆后相当于就进入了微信公众平台,在这个里面就可以抓取到需要的数据,如公众好友的昵称,fakeId。其中的fakeid非常重要,因为传输数据必须要知道对方的fakeid。
5.知道对方的fakeid就可以进行数据的发送了。
不过里面还有一些小问题,希望有人继续修改和讨论!也有人说这样会被封号,所以请谨慎操作
讲一下我项目里面的主要内容
1.WeiXinLogin.cs类是用来执行登陆功能的
//对密码进行MD5加密
staticstringGetMd5Str32(stringstr)
{
MD5CryptoServiceProvidermd5Hasher=newMD5CryptoServiceProvider();
//Converttheinputstringtoabytearrayandcomputethehash.
char[]temp=str.ToCharArray();
byte[]buf=newbyte[temp.Length];
for(inti=0;i<temp.Length;i++)
{
buf[i]=(byte)temp[i];
}
byte[]data=md5Hasher.ComputeHash(buf);
//CreateanewStringbuildertocollectthebytes
//andcreateastring.
StringBuildersBuilder=newStringBuilder();
//Loopthrougheachbyteofthehasheddata
//andformateachoneasahexadecimalstring.
for(inti=0;i<data.Length;i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
//Returnthehexadecimalstring.
returnsBuilder.ToString();
}
//执行登陆操作
publicstaticboolExecLogin(stringname,stringpass)
{
boolresult=false;
stringpassword=GetMd5Str32(pass).ToUpper();
stringpadata="username="+name+"&pwd="+password+"&imgcode=&f=json";
stringurl="http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";//请求登录的URL
try
{
CookieContainercc=newCookieContainer();//接收缓存
byte[]byteArray=Encoding.UTF8.GetBytes(padata);//转化
HttpWebRequestwebRequest2=(HttpWebRequest)WebRequest.Create(url); //新建一个WebRequest对象用来请求或者响应url
webRequest2.CookieContainer=cc; //保存cookie
webRequest2.Method="POST"; //请求方式是POST
webRequest2.ContentType="application/x-www-form-urlencoded"; //请求的内容格式为application/x-www-form-urlencoded
webRequest2.ContentLength=byteArray.Length;
StreamnewStream=webRequest2.GetRequestStream(); //返回用于将数据写入Internet资源的Stream。
//Sendthedata.
newStream.Write(byteArray,0,byteArray.Length); //写入参数
newStream.Close();
HttpWebResponseresponse2=(HttpWebResponse)webRequest2.GetResponse();
StreamReadersr2=newStreamReader(response2.GetResponseStream(),Encoding.Default);
stringtext2=sr2.ReadToEnd();
//此处用到了newtonsoft来序列化
WeiXinRetInforetinfo=Newtonsoft.Json.JsonConvert.DeserializeObject<WeiXinRetInfo>(text2);
stringtoken=string.Empty;
if(retinfo.ErrMsg.Length>0)
{
token=retinfo.ErrMsg.Split(newchar[]{"&"})[2].Split(newchar[]{"="})[1].ToString();//取得令牌
LoginInfo.LoginCookie=cc;
LoginInfo.CreateDate=DateTime.Now;
LoginInfo.Token=token;
result=true;
}
}
catch(Exceptionex)
{
thrownewException(ex.StackTrace);
}
returnresult;
}
publicstaticclassLoginInfo
{
///<summary>
///登录后得到的令牌
///</summary>
publicstaticstringToken{get;set;}
///<summary>
///登录后得到的cookie
///</summary>
publicstaticCookieContainerLoginCookie{get;set;}
///<summary>
///创建时间
///</summary>
publicstaticDateTimeCreateDate{get;set;}
}
2.在WeiXin.cs类中实现发送数据
publicstaticboolSendMessage(stringMessage,stringfakeid)
{
boolresult=false;
CookieContainercookie=null;
stringtoken=null;
cookie=WeiXinLogin.LoginInfo.LoginCookie;//取得cookie
token= WeiXinLogin.LoginInfo.Token;//取得token
stringstrMsg=System.Web.HttpUtility.UrlEncode(Message); //对传递过来的信息进行url编码
stringpadate="type=1&content="+strMsg+"&error=false&tofakeid="+fakeid+"&token="+token+"&ajax=1";
stringurl="https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN";
byte[]byteArray=Encoding.UTF8.GetBytes(padate);//转化
HttpWebRequestwebRequest2=(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(WindowsNT5.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1";
webRequest2.ContentType="application/x-www-form-urlencoded";
webRequest2.ContentLength=byteArray.Length;
StreamnewStream=webRequest2.GetRequestStream();
//Sendthedata.
newStream.Write(byteArray,0,byteArray.Length); //写入参数
newStream.Close();
HttpWebResponseresponse2=(HttpWebResponse)webRequest2.GetResponse();
StreamReadersr2=newStreamReader(response2.GetResponseStream(),Encoding.Default);
stringtext2=sr2.ReadToEnd();
if(text2.Contains("ok"))
{
result=true;
}
returnresult;
}
3.SendMessage.aspx.cs中主要实现获取fakeid
publicstaticArrayListSubscribeMP()
{
try
{
CookieContainercookie=null;
stringtoken=null;
cookie=WeiXinLogin.LoginInfo.LoginCookie;//取得cookie
token=WeiXinLogin.LoginInfo.Token;//取得token
/*获取用户信息的url,这里有几个参数给大家讲一下,1.token此参数为上面的token2.pagesize此参数为每一页显示的记录条数
3.pageid为当前的页数,4.groupid为微信公众平台的用户分组的组id,当然这也是我的猜想不一定正确*/
stringUrl="https://mp.weixin.qq.com/cgi-bin/contactmanagepage?t=wxm-friend&token="+token+"&lang=zh_CN&pagesize=10&pageidx=0&type=0&groupid=0";
HttpWebRequestwebRequest2=(HttpWebRequest)WebRequest.Create(Url);
webRequest2.CookieContainer=cookie;
webRequest2.ContentType="text/html;charset=UTF-8";
webRequest2.Method="GET";
webRequest2.UserAgent="Mozilla/5.0(WindowsNT5.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1";
webRequest2.ContentType="application/x-www-form-urlencoded";
HttpWebResponseresponse2=(HttpWebResponse)webRequest2.GetResponse();
StreamReadersr2=newStreamReader(response2.GetResponseStream(),Encoding.Default);
stringtext2=sr2.ReadToEnd();
MatchCollectionmc;
//由于此方法获取过来的信息是一个html网页所以此处使用了正则表达式,注意:(此正则表达式只是获取了fakeid的信息如果想获得一些其他的信息修改此处的正则表达式就可以了。)
Regexr=newRegex("\"fakeId\"\\s\\:\\s\"\\d+\"");//定义一个Regex对象实例
mc=r.Matches(text2);
Int32friendSum=mc.Count; //好友总数
stringfackID="";
ArrayListfackID1=newArrayList();
for(inti=0;i<friendSum;i++)
{
fackID=mc[i].Value.Split(newchar[]{":"})[1];
fackID=fackID.Replace("\"","").Trim();
fackID1.Add(fackID);
}
returnfackID1;
}
catch(Exceptionex)
{
thrownewException(ex.StackTrace);
}
}
相关文章
- 案例分享:C#基础教程之类class与结构struct的区别
- 在C#中如何List去除重复元素?
- C# 字符与字符串操作
- c#获得局域网主机列表实例
- asp.netc#抓取页面信息方法介绍
- C#动画窗体(AnimateWindow)的小例子
- C#利用ScriptControl动态执行JS和VBS脚本
- C#泛型与非泛型性能比较的实例
- 解析C#中的装箱与拆箱的详解
- c#方法中调用参数的值传递方式和引用传递方式以及ref与out的区别深入解析
- C#拓展方法的简单实例
- C#中通过API实现的打印类实例代码
- C#和asp.net中链接数据库中参数的几种传递方法实例代码
- C#加密解密类实例程序
- 探讨jQuery的ajax使用场景(c#)
- c#不使用windowsapi函数打开我的电脑和获取电脑驱动器信息
- c#获取光标在屏幕中位置的简单实例
- c#后台线程访问前台控件并显示信息示例
- C#装饰者模式实例分析
- C#定时关闭窗体实例
- c#分页读取GB文本文件实例
- C#转换日期类型实例
- C#中的委托介绍
- C#中父窗口和子窗口之间控件互操作实例
- 基于C#实现的三层架构实例
- C#实现百度网站收录和排名查询功能思路及实例
- 教你如何用C#制作文字转换成声音程序
- C#观察者模式(ObserverPattern)实例教程