Commit 3def1c02 by shenjunjie

添加Response close

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