|
@@ -70,13 +70,39 @@ public class HttpClientUtils {
|
|
return HttpClients.createDefault();
|
|
return HttpClients.createDefault();
|
|
}
|
|
}
|
|
|
|
|
|
- public String get(String urlStr) throws Exception {
|
|
|
|
|
|
+ public Boolean checkProxyIp(String ip, int port) {
|
|
|
|
+ int status = 0;
|
|
|
|
+ try {
|
|
|
|
+ SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
|
|
|
|
+ //信任所有证书
|
|
|
|
+ @Override
|
|
|
|
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }).build();
|
|
|
|
+ ConnectionConfig connectionConfig = ConnectionConfig.custom()
|
|
|
|
+ .setBufferSize(4096)
|
|
|
|
+ .build();
|
|
|
|
+ HttpHost proxy = new HttpHost(ip, port, "http");
|
|
|
|
+ RequestConfig globalConfig = RequestConfig.custom().setProxy(proxy).setConnectTimeout(1000).setSocketTimeout(1000).setCookieSpec(CookieSpecs.STANDARD).build();
|
|
|
|
+ SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
|
|
|
|
+
|
|
|
|
+ HttpClient httpClient = HttpClients.custom().setDefaultConnectionConfig(connectionConfig).setDefaultCookieStore(cookieStore).setDefaultRequestConfig(globalConfig).setConnectionReuseStrategy((response, context) -> false).setSSLSocketFactory(sslFactory).build();
|
|
|
|
+ HttpGet httpGet = new HttpGet("https://www.baidu.com");
|
|
|
|
+ status = httpClient.execute(httpGet).getStatusLine().getStatusCode();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.info("ip: " + ip + " is not aviable");
|
|
|
|
+ }
|
|
|
|
+ if (status == 200) {
|
|
|
|
+ return true;
|
|
|
|
+ } else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ public String get(String urlStr) throws Exception {
|
|
URL url = new URL(urlStr);
|
|
URL url = new URL(urlStr);
|
|
URLConnection urlConnection = url.openConnection(); // 打开连接
|
|
URLConnection urlConnection = url.openConnection(); // 打开连接
|
|
-
|
|
|
|
- System.out.println(urlConnection.getURL().toString());
|
|
|
|
-
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8")); // 获取输入流
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8")); // 获取输入流
|
|
String line = null;
|
|
String line = null;
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
@@ -104,14 +130,12 @@ public class HttpClientUtils {
|
|
Header[] headers = response.getHeaders("Set-Cookie");
|
|
Header[] headers = response.getHeaders("Set-Cookie");
|
|
if (headers != null && headers.length > 0) {
|
|
if (headers != null && headers.length > 0) {
|
|
for (Header header : headers) {
|
|
for (Header header : headers) {
|
|
- System.out.println(header.getValue());
|
|
|
|
String[] cookieArr = header.getValue().split(";");
|
|
String[] cookieArr = header.getValue().split(";");
|
|
String[] ck = cookieArr[0].split("=");
|
|
String[] ck = cookieArr[0].split("=");
|
|
BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
Map<String, String> cookieMap = new HashMap<>();
|
|
Map<String, String> cookieMap = new HashMap<>();
|
|
if (cookieArr.length > 1) {
|
|
if (cookieArr.length > 1) {
|
|
for (int i = 1; i < cookieArr.length; i++) {
|
|
for (int i = 1; i < cookieArr.length; i++) {
|
|
- System.out.println(cookieArr[i]);
|
|
|
|
String[] ckParam = cookieArr[i].split("=");
|
|
String[] ckParam = cookieArr[i].split("=");
|
|
if (ckParam.length > 1) {
|
|
if (ckParam.length > 1) {
|
|
cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
@@ -148,6 +172,7 @@ public class HttpClientUtils {
|
|
log.info(httpClientEntity.getResult());
|
|
log.info(httpClientEntity.getResult());
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
+ return httpGetRequest(httpClientEntity);
|
|
}
|
|
}
|
|
httpClientEntity.setResult(result);
|
|
httpClientEntity.setResult(result);
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
@@ -216,6 +241,7 @@ public class HttpClientUtils {
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
log.error(e.getMessage());
|
|
log.error(e.getMessage());
|
|
|
|
+ return httpGetRequest(httpClientEntity);
|
|
}
|
|
}
|
|
httpClientEntity.setResult(result);
|
|
httpClientEntity.setResult(result);
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
@@ -253,14 +279,12 @@ public class HttpClientUtils {
|
|
Header[] headers = response.getHeaders("Set-Cookie");
|
|
Header[] headers = response.getHeaders("Set-Cookie");
|
|
if (headers != null && headers.length > 0) {
|
|
if (headers != null && headers.length > 0) {
|
|
for (Header header : headers) {
|
|
for (Header header : headers) {
|
|
- System.out.println(header.getValue());
|
|
|
|
String[] cookieArr = header.getValue().split(";");
|
|
String[] cookieArr = header.getValue().split(";");
|
|
String[] ck = cookieArr[0].split("=");
|
|
String[] ck = cookieArr[0].split("=");
|
|
BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
Map<String, String> cookieMap = new HashMap<>();
|
|
Map<String, String> cookieMap = new HashMap<>();
|
|
if (cookieArr.length > 1) {
|
|
if (cookieArr.length > 1) {
|
|
for (int i = 1; i < cookieArr.length; i++) {
|
|
for (int i = 1; i < cookieArr.length; i++) {
|
|
- System.out.println(cookieArr[i]);
|
|
|
|
String[] ckParam = cookieArr[i].split("=");
|
|
String[] ckParam = cookieArr[i].split("=");
|
|
if (ckParam.length > 1) {
|
|
if (ckParam.length > 1) {
|
|
cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
@@ -283,6 +307,7 @@ public class HttpClientUtils {
|
|
log.info(httpClientEntity.getResult());
|
|
log.info(httpClientEntity.getResult());
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
|
+ return httpGetRequest(httpClientEntity);
|
|
}
|
|
}
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
return httpClientEntity;
|
|
return httpClientEntity;
|