天天看点

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

三、代码下载

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

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

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?

;

public

final

static

String INDEX_URL = 

"

http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&

;

public

final

static

String SENDMSG_URL = 

"

https://mp.weixin.qq.com/cgi-bin/singlesend"

;

public

final

static

String FANS_URL = 

"http://mp.weixin.qq.com/cgi-bin/contactmanagepage?t=wxm-friend&

;

public

final

static

String LOGOUT_URL = 

"

http://mp.weixin.qq.com/cgi-bin/logout?t=wxm-logout&

;

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 OPERATE_APPMSG = 

"https://mp.weixin.qq.com/cgi-bin/operate_appmsg?token=416919388&

;

public

final

static

String FMS_TRANSPORT = 

"

http://mp.weixin.qq.com/cgi-bin/fmstransport"

;

// public final static String CONTACT_MANAGE_PAGE =

// "http://mp.weixin.qq.com/cgi-bin/contactmanagepage";

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

final

static

String UPLOAD_MATERIAL = 

"https://mp.weixin.qq.com/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=2&token=416919388&t=iframe-uploadfile&

;

public

void

login() {}

private

boolean

_login() {}

private

String getToken(String s) {}

public

void

index() 

throws

HttpException, IOException {}

public

void

logout() 

throws

HttpException, IOException {}

public

InputStream code() 

throws

HttpException, IOException {}

public

int

getFans() {}

private

int

parseFansCount(String text) {}

private

int

parseFans(String text) {}

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 = 

"

[email protected]"

// 此为上一任作者的用户名和密码,截止到我最后用发现还能用

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模拟登录微信平台,主动推送消息给用户

继续阅读