|
@@ -3,6 +3,7 @@ package cn.com.ctop.job.bytedance.data.utils;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.gson.Gson;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.Header;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.HttpStatus;
|
|
@@ -12,6 +13,7 @@ import org.apache.http.client.config.CookieSpecs;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
import org.apache.http.conn.ssl.TrustStrategy;
|
|
@@ -21,6 +23,7 @@ import org.apache.http.impl.client.BasicCookieStore;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.impl.cookie.BasicClientCookie;
|
|
|
import org.apache.http.ssl.SSLContextBuilder;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
@@ -29,6 +32,7 @@ import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.net.URI;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.security.cert.CertificateException;
|
|
|
import java.security.cert.X509Certificate;
|
|
@@ -164,4 +168,70 @@ public class HttpUtils {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static HttpClient getHttpclient() {
|
|
|
+ RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(180000).setConnectTimeout(30000)
|
|
|
+ .setConnectionRequestTimeout(30000).setStaleConnectionCheckEnabled(true).build();
|
|
|
+ CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
|
|
|
+ return httpclient;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String httpGet(String url, Map<String, Object> paramsMap, Map<String, String> headers) {
|
|
|
+ String result = null;
|
|
|
+ try {
|
|
|
+ //创建参数列表
|
|
|
+ HttpClient httpclient = getHttpclient();
|
|
|
+ StringBuilder postBody = null;
|
|
|
+ if (!Check.isNull(paramsMap)) {
|
|
|
+ postBody = new StringBuilder();
|
|
|
+ for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
|
|
|
+ if (entry.getValue() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
|
|
|
+ "utf-8")).append("&");
|
|
|
+ }
|
|
|
+ if (!paramsMap.isEmpty()) {
|
|
|
+ postBody.deleteCharAt(postBody.length() - 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String urlStr;
|
|
|
+ if (!Check.isNull(postBody)) {
|
|
|
+ urlStr = url + "?" + postBody;
|
|
|
+ } else {
|
|
|
+ urlStr = url;
|
|
|
+ }
|
|
|
+ HttpGet httpget = new HttpGet(urlStr);
|
|
|
+// httpget.setHeader("User-Agent", USER_AGENT);
|
|
|
+ if (!Check.isNullMap(headers)) {
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ httpget.setHeader(key, headers.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpEntity respEntity;
|
|
|
+ HttpResponse response = httpclient.execute(httpget);
|
|
|
+ respEntity = response.getEntity();
|
|
|
+ Header[] responseHeaders = response.getHeaders("Set-Cookie");
|
|
|
+ if (responseHeaders != null) {
|
|
|
+
|
|
|
+ for (Header header : responseHeaders) {
|
|
|
+ String[] ckArray = header.getValue().split(";");
|
|
|
+ BasicClientCookie ck = new BasicClientCookie(ckArray[0].split("=")[0], ckArray[0].split("=")[1]);
|
|
|
+ ck.setPath(ckArray[1].split("=")[1]);
|
|
|
+ ck.setDomain(ckArray[2].split("=")[1]);
|
|
|
+ HttpUtils.COOKIESTORE.addCookie(ck);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result = EntityUtils.toString(respEntity);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("http get请求异常");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|