package com.qunfa.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.regex.Pattern;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import com.qunfa.model.UserType;
import com.qunfa.model.WxUser;
public class WxService {
public String token;
public String slave_user;
public String slave_sid;
public String sig;
public byte[] getVerifycode(String username, HttpServletRequest request) {
try {
String url = "https://mp.weixin.qq.com/cgi-bin/verifycode?username="
+ username + "&r=" + System.currentTimeMillis();
HttpClient client = new HttpClient();
Protocol myhttps = new Protocol("https",
new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
GetMethod getMethod = new GetMethod(url);
client.executeMethod(getMethod);
Cookie[] cookies = client.getState().getCookies();
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("sig")) {
sig = cookies[i].getValue();
request.getSession().setAttribute("sig", sig);
}
}
return getMethod.getResponseBody();
} catch (Exception e) {
return null;
}
}
public String md5(String pwd) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine engine = m.getEngineByName("javascript");
InputStreamReader in = new InputStreamReader(new WxService().getClass()
.getResourceAsStream("/com/qunfa/common/txmd5.js"));
BufferedReader read = new BufferedReader(in);
engine.eval(read);
Invocable invocableEngine = (Invocable) engine;
return invocableEngine.invokeFunction("md5", pwd).toString();
}
public boolean login(String username, String pwd, String yzm,
HttpServletRequest request) {
try {
HttpSession session = request.getSession();
String url = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN&f=json&imgcode="
+ yzm + "&pwd=" + md5(pwd) + "&username=" + username;
HttpClient client = new HttpClient();
Protocol myhttps = new Protocol("https",
new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
PostMethod getMethod = new PostMethod(url);
if (!yzm.equals("")) {
getMethod.setRequestHeader("Cookie", "sig="
+ session.getAttribute("sig"));
}
getMethod.setRequestHeader("Referer","https://mp.weixin.qq.com/");
getMethod.setRequestHeader("Host","mp.weixin.qq.com");
client.executeMethod(getMethod);
Cookie[] cookies = client.getState().getCookies();
String returnStr = getMethod.getResponseBodyAsString();
token = returnStr.split("token=")[1].split("\"")[0];
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("slave_user")) {
slave_user = cookies[i].getValue();
} else if (cookies[i].getName().equals("slave_sid")) {
slave_sid = cookies[i].getValue();
}
}
session.setAttribute("token", token);
session.setAttribute("slave_user", slave_user);
session.setAttribute("slave_sid", slave_sid);
return true;
} catch (Exception e) {
return false;
}
}
public ArrayList<UserType> getUserTypeList(HttpServletRequest request) {
ArrayList<UserType> userTypeList = new ArrayList<UserType>();
try {
HttpSession session = request.getSession();
token = session.getAttribute("token").toString();
slave_user = session.getAttribute("slave_user").toString();
slave_sid = session.getAttribute("slave_sid").toString();
String url = "https://mp.weixin.qq.com/cgi-bin/contactmanagepage?t=wxm-friend&token="
+ token
+ "&lang=zh_CN&pagesize=20&pageidx=0&type=0&groupid=0";
HttpClient client = new HttpClient();
Protocol myhttps = new Protocol("https",
new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
GetMethod getMethod = new GetMethod(url);
getMethod.setRequestHeader("Cookie", "slave_user=" + slave_user
+ ";slave_sid=" + slave_sid);
client.executeMethod(getMethod);
String returnStr = getMethod.getResponseBodyAsString();
String fzxx = "{\"message\":" + returnStr.split("DATA.groupList =")[1].split("];")[0]+"]}";
System.out.println(getJson(fzxx));
JSONObject jsonob = JSONObject.fromObject(getJson(fzxx).replaceAll(",,", ","));
JSONArray jsons = jsonob.getJSONArray("message");
JSONObject tempJson;
UserType userType = null;
for (int i = 0; i < jsons.size(); i++) {
userType = new UserType();
tempJson = JSONObject.fromObject(jsons.get(i));
userType.setId(tempJson.get("id").toString());
userType.setName(tempJson.get("name").toString());
userType.setNum(Integer.parseInt(tempJson.get("num").toString().split("'")[0]));
userTypeList.add(userType);
}
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("userTypeList",userTypeList);
return userTypeList;
}
//解析成json格式
private static String getJson(String json) {
return Pattern.compile("defaultGroupName\\[\\d+]\\s+\\|*").matcher(json).replaceAll("").replaceAll("\\*1", "");
}
public boolean getFanList(HttpServletRequest request) {
ArrayList<WxUser> userList = new ArrayList<WxUser>();
try {
int PageIdx = 1; //当前页码
String pgn = request.getParameter("pgn");
if(pgn!=null&&!"".equals(pgn)){
PageIdx = Integer.parseInt(pgn);
}
Page page = new Page();
HttpSession session = request.getSession();
token = session.getAttribute("token").toString();
slave_user = session.getAttribute("slave_user").toString();
slave_sid = session.getAttribute("slave_sid").toString();
String url = "https://mp.weixin.qq.com/cgi-bin/contactmanagepage?t=wxm-friend&token="
+ token
+ "&lang=zh_CN&pagesize=10&pageidx="+(PageIdx-1)+"&type=0&groupid=0";
HttpClient client = new HttpClient();
Protocol myhttps = new Protocol("https",
new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
GetMethod getMethod = new GetMethod(url);
getMethod.setRequestHeader("Cookie", "slave_user=" + slave_user
+ ";slave_sid=" + slave_sid);
client.executeMethod(getMethod);
String returnStr = getMethod.getResponseBodyAsString();
// 获取分页等信息
String fzxx = "{\"message\":["
+ returnStr.split("DATA.List.pageNav =")[1].split("};")[0]
+ "}]}";
fzxx = fzxx.replaceAll("[*]1", "");
JSONObject jsonob = JSONObject.fromObject(fzxx);
JSONArray jsons = jsonob.getJSONArray("message");
JSONObject tempJson;
int PageCount = 0; //总页数
int PageSize = 0; //每页数量
for (int i = 0; i < jsons.size(); i++) {
tempJson = JSONObject.fromObject(jsons.get(i));
PageCount = Integer.parseInt(tempJson.get("PageCount")
.toString());
PageSize = Integer
.parseInt(tempJson.get("PageSize").toString());
}
page.setPvar("pgn");
page.setFilePath("fanList.jsp");
page.setCharSet("GBK");
page.set(PageCount,PageIdx,PageSize);
Document doc = Jsoup.parse(returnStr);
Elements elements = doc.select("script[id=json-friendList]");
if (elements != null) {
String json = elements.toString().replace(
"<script id=\"json-friendList\" type=\
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
(java).rar (86个子文件)
qunfa
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 224B
org.eclipse.wst.common.component 453B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 395B
src
com
qunfa
model
FanListDTO.java 453B
WxUser.java 362B
UserType.java 641B
servlet
SendMessageServlet.java 2KB
LoginServlet.java 3KB
common
Page.java 4KB
MySSLProtocolSocketFactory.java 3KB
WxService.java 10KB
txmd5.js 6KB
.project 2KB
WebRoot
images
index-nav-right.png 4KB
login-btn.gif 3KB
index-footer-bg.gif 152B
index-nav-firstbg.png 3KB
index-menu-title.gif 2KB
index-subnav-bg.gif 153B
index-submenu-bg.gif 1KB
index-nav-left.png 4KB
login-bg.png 42KB
page-pageTitle-bg.gif 155B
index-logout.png 4KB
index-nav-bg.png 3KB
sidebar-on.gif 177B
login-bg.gif 97KB
index-logo.gif 8KB
index-menu-bg.gif 2KB
index-subnav-right.gif 422B
page-bg.gif 190B
login-input.gif 3KB
index-logo.psd 118KB
icon
setup.png 686B
del.png 678B
edit.png 879B
edit2.png 823B
error2.png 653B
error3.png 673B
delete.png 957B
save.png 429B
error.png 764B
index-menu-title.png 4KB
index-header-bg.gif 274B
index-menu-bg2.gif 73B
index-subnav-left.gif 419B
style.css 7KB
login.css 2KB
page-thead-bg.gif 285B
main.html 1KB
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 1KB
classes
com
qunfa
model
WxUser.class 725B
UserType.class 1KB
FanListDTO.class 1KB
servlet
LoginServlet.class 3KB
SendMessageServlet.class 2KB
common
MySSLProtocolSocketFactory$TrustAnyTrustManager.class 1KB
WxService.class 11KB
Page.class 4KB
MySSLProtocolSocketFactory.class 4KB
txmd5.js 6KB
lib
ezmorph-1.0.6.jar 84KB
commons-beanutils.jar 184KB
commons-collections.jar 558KB
json-lib-2.2.1-jdk15.jar 137KB
commons-httpclient.jar 273KB
commons-logging.jar 52KB
jstl-1.2.jar 405KB
commons-codec.jar 46KB
commons-lang.jar 237KB
commons-lang3-3.1.jar 308KB
jsoup-1.7.2.jar 287KB
js
jquery.min.js 93KB
windows.js 11KB
login.jsp 3KB
table.html 3KB
fanList.jsp 4KB
index.jsp 3KB
userTypeList.jsp 3KB
.mymetadata 288B
.classpath 1KB
共 86 条
- 1
低0调
- 粉丝: 7
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页