|
@@ -9,6 +9,7 @@ import org.apache.http.client.CookieStore;
|
|
import org.apache.http.client.HttpClient;
|
|
import org.apache.http.client.HttpClient;
|
|
import org.apache.http.client.config.CookieSpecs;
|
|
import org.apache.http.client.config.CookieSpecs;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
import org.apache.http.conn.ssl.TrustStrategy;
|
|
import org.apache.http.conn.ssl.TrustStrategy;
|
|
@@ -20,6 +21,7 @@ import org.apache.http.ssl.SSLContextBuilder;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
+import java.net.URLEncoder;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
import java.security.cert.CertificateException;
|
|
import java.security.cert.CertificateException;
|
|
import java.security.cert.X509Certificate;
|
|
import java.security.cert.X509Certificate;
|
|
@@ -140,4 +142,56 @@ public class HttpUtils {
|
|
}
|
|
}
|
|
return strReturn;
|
|
return strReturn;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static String httpGet(String url, Map<String, Object> paramsMap, Map<String, String> headers) {
|
|
|
|
+ String result = null;
|
|
|
|
+ try {
|
|
|
|
+ // CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
|
+ //创建参数列表
|
|
|
|
+ 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();
|
|
|
|
+ result = EntityUtils.toString(respEntity);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("http get请求异常");
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static HttpClient getHttpclient() {
|
|
|
|
+ RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(180000).setConnectTimeout(30000)
|
|
|
|
+ .setConnectionRequestTimeout(30000).setStaleConnectionCheckEnabled(true).build();
|
|
|
|
+ return HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
|
|
|
|
+ }
|
|
}
|
|
}
|