Commit 3def1c02 by shenjunjie

添加Response close

parent 0348e2a1
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.zhiwei</groupId> <groupId>com.zhiwei</groupId>
<artifactId>wechat</artifactId> <artifactId>wechat</artifactId>
<version>1.3.4-SNAPSHOT</version> <version>1.3.6-SNAPSHOT</version>
<description> <description>
知微微信采集程序,包含 知微微信采集程序,包含
1.微信历史文章采集 1.微信历史文章采集
......
...@@ -61,29 +61,48 @@ public class WechatReal { ...@@ -61,29 +61,48 @@ public class WechatReal {
} }
headers.put("cookie", cookie); headers.put("cookie", cookie);
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
String htmlBody = httpBoot.syncCall(RequestUtils.wrapGet(url, headers), proxy).body().string(); Response response = null;
if (StringUtils.isNotBlank(htmlBody) && !htmlBody.contains("验证码")) { try {
response = httpBoot.syncCall(RequestUtils.wrapGet(url, headers), proxy);
String htmlBody = response.body().string();
// cookie过期
if (StringUtils.isNotBlank(htmlBody) || htmlBody.contains("验证码")) {
initCookie(url, headers, proxy);
continue;
}
StringBuilder furl = new StringBuilder(); StringBuilder furl = new StringBuilder();
Matcher ma1 = PATTERN.matcher(htmlBody); Matcher ma1 = PATTERN.matcher(htmlBody);
while (ma1.find()) { while (ma1.find()) {
furl.append(ma1.group(1)); furl.append(ma1.group(1));
} }
return furl.toString(); return furl.toString();
} else { } catch (Exception e) {
// cookie过期 logger.error(e);
initCookie(url, headers, proxy); } finally {
if (null != response) {
response.close();
}
} }
} }
throw new NullPointerException("ip:" + proxy.address() + "获取临时链接失败, 出现输入验证码"); throw new NullPointerException("ip:" + proxy.address() + "获取临时链接失败, 出现输入验证码");
} }
private void initCookie(String url, Map<String, Object> headers, Proxy proxy) throws IOException { private void initCookie(String url, Map<String, Object> headers, Proxy proxy) {
Request request = RequestUtils.wrapGet(getSearchUrl(url), headers); Response response = null;
Response response = httpBoot.syncCall(request, proxy); try {
// System.out.println("htmlBody:" + response.body().string()); Request request = RequestUtils.wrapGet(getSearchUrl(url), headers);
cookie = response.headers("set-cookie").stream().map(s -> s.replaceAll(";.*", "")) response = httpBoot.syncCall(request, proxy);
.collect(Collectors.joining("; ")); // System.out.println("htmlBody:" + response.body().string());
logger.info("ip:{},初始化cookie:{}", proxy.address(), cookie); cookie = response.headers("set-cookie").stream().map(s -> s.replaceAll(";.*", ""))
.collect(Collectors.joining("; "));
logger.info("ip:{},初始化cookie:{}", proxy.address(), cookie);
} catch (Exception e) {
logger.info("ip:{},初始化cookie异常:", proxy.address(), e);
} finally {
if (null != response) {
response.close();
}
}
} }
/** /**
...@@ -101,24 +120,24 @@ public class WechatReal { ...@@ -101,24 +120,24 @@ public class WechatReal {
timestamp, "&lkt=1%2C", timestamp, "%2C", timestamp); timestamp, "&lkt=1%2C", timestamp, "%2C", timestamp);
} }
/** // /**
* 获取转链接的中间跳转链接 // * 获取转链接的中间跳转链接
* // *
* @param originalUrl // * @param originalUrl
* @return // * @return
*/ // */
@Deprecated // @Deprecated
private static String getOriginalUrl(String originalUrl) { // private static String getOriginalUrl(String originalUrl) {
int b = (int) (Math.floor(100 * Math.random()) + 1); // int b = (int) (Math.floor(100 * Math.random()) + 1);
int a = originalUrl.indexOf("url="); // int a = originalUrl.indexOf("url=");
int c = originalUrl.indexOf("&k="); // int c = originalUrl.indexOf("&k=");
String d = null; // String d = null;
if (a != -1 && -1 == c) { // if (a != -1 && -1 == c) {
d = originalUrl.substring(a + 25 + b, a + 26 + b); // d = originalUrl.substring(a + 25 + b, a + 26 + b);
} // }
originalUrl += "&k=" + b + "&h=" + d; // originalUrl += "&k=" + b + "&h=" + d;
return originalUrl; // return originalUrl;
} // }
// public static void main(String[] args) throws Exception { // public static void main(String[] args) throws Exception {
// WechatReal real = new WechatReal(); // WechatReal real = new WechatReal();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment