package com.weixin;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* @author TonyG
*
*/
public class WeiXinClient {
private final static Log log = LogFactory.getLog(WeiXinClient.class);
//目标主机
public final static String HOST = "https://mp.weixin.qq.com";
//登陆页
public final static String LOGIN_URL = "/cgi-bin/login?lang=zh_CN";
//登录后首页
public final static String INDEX_URL = "/cgi-bin/home?t=home/index&lang=zh_CN";
//单个发送消息
public final static String SINGLE_SEND_URL="/cgi-bin/singlesend?t=ajax-response&lang=zh_CN";
public final static String SINGLE_SEND_PAGE="/cgi-bin/singlesendpage?t=message/send&action=index&lang=zh_CN";
//群发
public final static String MASS_SEND_URL="/cgi-bin/masssend?t=ajax-response";
public final static String MASS_SEND_PAGE="/cgi-bin/masssendpage?t=wxm-send&lang=zh_CN";
//粉丝页面,用户管理
public final static String FANS_URL = "/cgi-bin/contactmanage?t=user/index&lang=zh_CN&pagesize=10&type=0";
public final static String FANS_INFO="/cgi-bin/getcontactinfo";
public final static String UTF_8 = "UTF-8";
public final static String REFERER="Referer";
public final static String COOKIE="Cookie";
private DefaultHttpClient client = HttpManager.getHttpClient();//WeiXinHttpsClient.getHttpClient();
private String cookiestr;
private String token;
private String loginErrMsg;
private String loginUser;
private String loginPwd;
private boolean isMd5=false;
private boolean isLogin = false;
private Map<String,FansInfo> allFansMap=new HashMap<String,FansInfo>();
private Map<String,String> userFakeIdMap=new HashMap<String,String>();
/**
* 获取weixin模拟客户端
* @param user 用户名
* @param pwd 密码
* @param isMd5 密码是否已经md5加密,true已加密,false未加密
* @return
*/
public WeiXinClient(String user, String pwd,boolean isMd5) {
this.loginUser = user;
this.loginPwd = pwd;
this.isMd5=isMd5;
}
public Map<String, String> getUserFakeIdMap() {
return userFakeIdMap;
}
public Map<String, FansInfo> getAllFansMap() {
return allFansMap;
}
/**
* 登录,登录失败会重复请求登录
*/
public void login() {
boolean bool = checkLogin();
while (!bool) {
String info = "【登录失败】【错误代码:" + this.loginErrMsg + "】【账号:"
+ this.loginUser + "】正在尝试重新登录....";
log.debug(info);
System.out.println(info);
bool = checkLogin();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
bool = checkLogin();
}
}
}
/**
* 发送登录信息,记录cookie,登录状态,token等信息
*
* @return
*/
private boolean checkLogin() {
try {
HttpPost post = new HttpPost(HOST+LOGIN_URL);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
//post 参数 传递
params.add(new BasicNameValuePair("username", this.loginUser));
params.add(new BasicNameValuePair("pwd", !isMd5?DigestUtils.md5Hex(this.loginPwd.getBytes()):this.loginPwd));
params.add(new BasicNameValuePair("f", "json"));
params.add(new BasicNameValuePair("imagecode", ""));
post.setEntity(new UrlEncodedFormEntity(params, UTF_8)); //设置参数给Post
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
response.getEntity().writeTo(baos);
String ret = baos.toString();
LoginJson retcode = JSON.parseObject(ret, LoginJson.class);
if (retcode.getRet() == 302 && retcode.getErrCode() == 0) {
StringBuffer cookie = new StringBuffer();
for (Cookie c : client.getCookieStore().getCookies()) {
cookie.append(c.getName()).append("=")
.append(c.getValue()).append(";");
}
this.cookiestr = cookie.toString();
this.isLogin = true;
this.token = splitToken(retcode.getErrMsg());
System.out.println(this.loginUser+" 登陆成功!");
return true;
}
int errCode = retcode.getErrCode();
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 = "【登录失败】【发生异常
- 1
- 2
- 3
- 4
前往页