51,410
社区成员
发帖
与我相关
我的任务
分享
public static String TOKEN;
public static Map<String, String> cookies;
public static String LOGIN_URL = "http://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
public static String SEND_MSG = "http://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN";
public final static String USER_AGENT_H = "User-Agent";
public final static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22";
public final static String REFERER_H = "Referer";
public final static String INDEX_URL = "http://mp.weixin.qq.com/cgi-bin/indexpage?t=wxm-index&lang=zh_CN";
public final static String INDEX_URL2 = "https://mp.weixin.qq.com/";
/**
* 获取登录session
* @return
*/
public static void auth(String username, String password) throws IOException {
Map<String, String> map = new HashMap<String, String>();
map.put("username", username);
map.put("pwd", MD5.getMD5(password.substring(0, password.length()).getBytes()).toUpperCase());
map.put("f", "json");
map.put("imagecode", "");
Response response = Jsoup.connect(LOGIN_URL).header(USER_AGENT_H, USER_AGENT).header(REFERER_H, INDEX_URL2).ignoreContentType(true)
.method(Method.POST).data(map).execute();
// Map<String, String> cookies = response.cookies();
//新添加代码
String json = response.body();
System.out.println(json);
Gson gson = new Gson();
WeiXinResponse weiXinResponse = gson.fromJson(json, WeiXinResponse.class);
String errMsg = weiXinResponse.getErrMsg();
TOKEN = errMsg.substring(errMsg.lastIndexOf("=")+1, errMsg.length());
cookies = response.cookies();
// return cookies;
}
二.
/**
* 发送登录信息,记录cookie,登录状态,token等信息
*
* @return
*/
public boolean login() {
try {
PostMethod post = new PostMethod(LOGIN_URL);
post.setRequestHeader(USER_AGENT_H, USER_AGENT);
post.setRequestHeader(REFERER_H, INDEX_URL);
post.setRequestHeader(REFERER_H, INDEX_URL);
NameValuePair[] params = new NameValuePair[]{
new NameValuePair("username", this.loginUser),
new NameValuePair("pwd", DigestUtils.md5Hex(this.loginPwd
.getBytes())), new NameValuePair("f", "json"),
new NameValuePair("imagecode", "")};
post.setQueryString(params);
client.getHostConfiguration().setProxy("172.19.196.252", 8080);
int status = client.executeMethod(post);
if (status == HttpStatus.SC_OK) {
String ret = post.getResponseBodyAsString();
LoginJson retcode = JSON.parseObject(ret, LoginJson.class);
System.out.println(retcode.getRet());
if (retcode.getRet() == 302 && retcode.getErrCode() == 0) {
this.cookies = client.getState().getCookies();
StringBuffer cookie = new StringBuffer();
for (Cookie c : client.getState().getCookies()) {
cookie.append(c.getName()).append("=")
.append(c.getValue()).append(";");
}
this.cookiestr = cookie.toString();
this.isLogin = true;
this.token = getToken(retcode.getErrMsg());
return true;
}
int errCode = retcode.getErrCode();
this.loginErrCode = errCode;
switch (errCode) {
case -1:
this.loginErrMsg = "系统错误";
return false;
case -2:
this.loginErrMsg = "帐号或密码错误";
return false;
case -3:
this.loginErrMsg = "密码错误";
return false;
case -4:
this.loginErrMsg = "不存在该帐户";
return false;
case -5:
this.loginErrMsg = "访问受限";
return false;
case -6:
this.loginErrMsg = "需要输入验证码";
return false;
case -7:
this.loginErrMsg = "此帐号已绑定私人微信号,不可用于公众平台登录";
return false;
case -8:
this.loginErrMsg = "邮箱已存在";
return false;
case -32:
this.loginErrMsg = "验证码输入错误";
return false;
case -200:
this.loginErrMsg = "因频繁提交虚假资料,该帐号被拒绝登录";
return false;
case -94:
this.loginErrMsg = "请使用邮箱登陆";
return false;
case 10:
this.loginErrMsg = "该公众会议号已经过期,无法再登录使用";
return false;
case 65201:
case 65202:
this.loginErrMsg = "成功登陆,正在跳转...";
return true;
case 0:
this.loginErrMsg = "成功登陆,正在跳转...";
return true;
default:
this.loginErrMsg = "未知的返回";
return false;
}
}
} catch (Exception e) {
String info = "【登录失败】【发生异常:" + e.getMessage() + "】";
System.err.println(info);
log.debug(info);
log.info(info);
return false;
}
return false;
}


大神们,跑到哪里去鸟。。?