Java模拟登录微信平台,主动推送消息给用户,自动绑定url、token等

一、简要说明

在博文《Java模拟登录微信公众平台,主动推送图文消息给用户》中提到使用Java语言登录微信公众平台,然后发送图文消息给用户,基本可以符合使用要求,但是在今年10月23日,微信公众平台的界面有所变化,所以,上一篇博文的代码不再适用,很多朋友留言,不能使用了怎么办,这篇文章针对新版本做了处理,并且加入了新的功能,代码上要感谢trprebel这位朋友。

二、代码功能和要求

*.new Weixin()对象,先登录再取粉丝数和者发消息;

*.发消息需要设置post参数中的content;

*.内容中的超链接可以直接发送不用使用标签;

*.经过我(trprebel)修改之后,此份代码可在2013年11月之后使用;

*.我只做了获取粉丝列表和发送消息,其他部分未做;

*.理论上可以获取到粉丝的地址,签名等一切你登陆可以得到的信息;

*.另外可能需要你在本机先登陆过至少一次微信公众平台获取SSL证书;

*.此份代码作者较多,函数前面都有作者名,我只修改了登陆,获取粉丝列表和发送消息;

*.其他代码可能已经不能用了但我并没有删除,方便大家扩展,我做的也比较粗糙,没整理;

*.另外,腾讯3天之内又加了一条限制,粉丝24小时之内没有主动说话,微信公众平台不能主动与其说话,官方平台登陆也不行;

使用到的库:commons-codec-1.3.jar、commons-httpclient-3.1.jar、commons-lang.jar、commons-logging-1.0.4.jar、fastjson-1.1.15.jar、gson-2.2.4.jar、httpclient-4.1.3.jar、httpcore-4.1.4.jar、jsoup-1.5.2.jar

环境:JDK1.6

三、代码下载

使用的库请自己下载,源代码如下:

SimulateWechatLogin.zip

如果使用有任何的问题,请直接回复博文即可...

四、核心代码和说明

Weixin.java模拟请求的链接及结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
public  final  static  String HOST =  "http://mp.weixin.qq.com" ;
public  final  static  String LOGIN_URL =  "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN" ;
public  final  static  String INDEX_URL =  "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN" ;
public  final  static  String SENDMSG_URL =  "https://mp.weixin.qq.com/cgi-bin/singlesend" ;
public  final  static  String LOGOUT_URL =  "http://mp.weixin.qq.com/cgi-bin/logout?t=wxm-logout&lang=zh_CN" ;
public  final  static  String DOWNLOAD_URL =  "http://mp.weixin.qq.com/cgi-bin/downloadfile?" ;
public  final  static  String VERIFY_CODE =  "http://mp.weixin.qq.com/cgi-bin/verifycode?" ;
public  final  static  String POST_MSG =  "https://mp.weixin.qq.com/cgi-bin/masssend?t=ajax-response" ;
public  final  static  String VIEW_HEAD_IMG =  "http://mp.weixin.qq.com/cgi-bin/viewheadimg" ;
public  final  static  String GET_IMG_DATA =  "http://mp.weixin.qq.com/cgi-bin/getimgdata" ;
public  final  static  String GET_REGIONS =  "http://mp.weixin.qq.com/cgi-bin/getregions" ;
public  final  static  String GET_MESSAGE =  "http://mp.weixin.qq.com/cgi-bin/getmessage" ;
public  final  static  String OPER_ADVANCED_FUNC =  "http://mp.weixin.qq.com/cgi-bin/operadvancedfunc" ;
public  final  static  String MASSSEND_PAGE =  "http://mp.weixin.qq.com/cgi-bin/masssendpage" ;
public  final  static  String FILE_MANAGE_PAGE =  "http://mp.weixin.qq.com/cgi-bin/filemanagepage" ;
public  final  static  String FMS_TRANSPORT =  "http://mp.weixin.qq.com/cgi-bin/fmstransport" ;
// public final static String CONTACT_MANAGE_PAGE =
public  final  static  String CONTACT_MANAGE_PAGE =  "http://mp.weixin.qq.com/cgi-bin/contactmanage" ;
public  final  static  String OPER_SELF_MENU =  "http://mp.weixin.qq.com/cgi-bin/operselfmenu" ;
public  final  static  String REPLY_RULE_PAGE =  "http://mp.weixin.qq.com/cgi-bin/replyrulepage" ;
public  final  static  String SINGLE_MSG_PAGE =  "http://mp.weixin.qq.com/cgi-bin/singlemsgpage" ;
public  final  static  String USER_INFO_PAGE =  "http://mp.weixin.qq.com/cgi-bin/userinfopage" ;
public  final  static  String DEV_APPLY =  "http://mp.weixin.qq.com/cgi-bin/devapply" ;
/**
* 登录,登录失败会重复请求登录
*/
public  void  login() {}
/**
* 发送登录信息,记录cookie,登录状态,token等信息
*/
private  boolean  _login() {}
/**
* 从登录成功的信息中分离出token信息
*/
private  String getToken(String s) {}
/**
* 获取首页
* @throws HttpException
* @throws IOException
*/
public  void  index()  throws  HttpException, IOException {}
/**
* 登出操作
* @throws HttpException
* @throws IOException
*/
public  void  logout()  throws  HttpException, IOException {}
/**
* 获取验证码
* @throws HttpException
* @throws IOException
*/
public  InputStream code()  throws  HttpException, IOException {}
/**
* 获取粉丝列表,返回粉丝数量,出错则返回-1
*/
public  int  getFans() {}
/**
* 从返回文本中提取粉丝数量
*/
private  int  parseFansCount(String text) {}
/**
* 解析粉丝列表,将粉丝列表存入List<fan>
*/
private  int  parseFans(String text) {}
/**
* <strong>群发消息</strong>
* 返回码说明
* 0:发送成功
* 64004:今天的群发数量已到,无法群发
* -20000:请求被禁止,请仔细检查token是否合法
* 可通过msgSendCode取得发送状态码
*/
public  boolean  msgSend(MsgForm form, MsgType type) {}
/**
* 向粉丝发送消息,默认发送第二个粉丝,想不受限制的群发可以循环向粉丝列表中的粉丝发送消息
*/
public  boolean  sendMsg( int  i) {}
public  void  updateImg(ImgFileForm form) {}
/**
* 页面跳转
*/
public  void  redirect(String url) {}
//使用样例说明
public  static  void  main(String[] args) {
     String LOGIN_USER =  "knight.ding@gmail.com" // 此为上一任作者的用户名和密码,截止到我最后用发现还能用
     String LOGIN_PWD =  "AAbb1122" ;
     Weixin wx =  new  Weixin(LOGIN_USER, LOGIN_PWD);
     wx.login();
     wx.getCookiestr();
     // ImgFileForm form = new ImgFileForm();
     // form.setUploadfile(new File("D:Dataimage4.jpg"));
     // wx.updateImg(form);
     System.out.println( "粉丝数:"  + wx.getFans());
     wx.sendMsg( 1 ); // 像好友列表中的第几个好友发消息,从0开始
}

---

Enjoy~~~如果能帮助到你,请回复博文或者分享给他人,谢谢~


转载出处:【微信公众平台改版后】Java模拟登录微信平台,主动推送消息给用户

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值