|
@@ -9,6 +9,8 @@ import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
|
|
+import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
@@ -21,6 +23,8 @@ import org.slf4j.LoggerFactory;
|
|
|
import javax.net.ssl.*;
|
|
|
import java.io.*;
|
|
|
import java.net.*;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
import java.util.*;
|
|
|
|
|
@@ -112,7 +116,12 @@ public class HttpUtils
|
|
|
return result.toString();
|
|
|
}
|
|
|
public static String postFormBody(String url, Map<String, String> paramMap) throws IOException{
|
|
|
- return post(url, paramMap, null);
|
|
|
+ try{
|
|
|
+ String result = post(url, paramMap, null);
|
|
|
+ return result;
|
|
|
+ }catch (Exception e){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
@@ -470,7 +479,7 @@ public class HttpUtils
|
|
|
* @return 请求返回的数据
|
|
|
* @throws IOException 读写异常
|
|
|
*/
|
|
|
- private static String post(String url, Map<String, String> paramMap, String encoding) throws IOException {
|
|
|
+ private static String post(String url, Map<String, String> paramMap, String encoding) throws IOException, NoSuchAlgorithmException, KeyManagementException {
|
|
|
log.debug("http 请求 url: {} , 请求参数: {}", url, appendUrl("", paramMap).replace("?", ""));
|
|
|
encoding = encoding == null ? UTF8 : encoding;
|
|
|
// 创建post方式请求对象
|
|
@@ -512,11 +521,28 @@ public class HttpUtils
|
|
|
* @throws IOException 读写异常
|
|
|
*/
|
|
|
private static <T> T post(String url, HttpPost httpPost, String encoding, DataParse<T> dataParse)
|
|
|
- throws IOException {
|
|
|
+ throws IOException, NoSuchAlgorithmException, KeyManagementException {
|
|
|
+ TrustManager[] trustAllCerts = new TrustManager[] {
|
|
|
+ new X509TrustManager() {
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
|
|
+ // don't check
|
|
|
+ }
|
|
|
+ public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
|
|
+ // don't check
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ SSLContext ctx = SSLContext.getInstance("TLS");
|
|
|
+ ctx.init(null, trustAllCerts, null);
|
|
|
+ LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(ctx);
|
|
|
T result = null;
|
|
|
CloseableHttpResponse response = null;
|
|
|
// 创建httpclient对象
|
|
|
- CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
+ CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslSocketFactory).build();
|
|
|
|
|
|
// 执行请求操作,并拿到结果(同步阻塞)
|
|
|
response = sendRequestAndGetResult(url, httpClient, httpPost);
|
|
@@ -599,8 +625,12 @@ public class HttpUtils
|
|
|
}
|
|
|
|
|
|
public static String postFormBodyHeader(String url, Map<String, String> paramMap, Map<String, String> headersMap, Header[] header) throws IOException{
|
|
|
- return postHearder(url, paramMap, null, headersMap, header);
|
|
|
-
|
|
|
+ try{
|
|
|
+ String result = postHearder(url, paramMap, null, headersMap, header);
|
|
|
+ return result;
|
|
|
+ }catch (Exception e){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -612,7 +642,7 @@ public class HttpUtils
|
|
|
* @return 请求返回的数据
|
|
|
* @throws IOException 读写异常
|
|
|
*/
|
|
|
- private static String postHearder(String url, Map<String, String> paramMap, String encoding, Map<String, String> headersMap, Header[] header) throws IOException {
|
|
|
+ private static String postHearder(String url, Map<String, String> paramMap, String encoding, Map<String, String> headersMap, Header[] header) throws IOException, NoSuchAlgorithmException, KeyManagementException {
|
|
|
log.debug("http 请求 url: {} , 请求参数: {}", url, appendUrl("", paramMap).replace("?", ""));
|
|
|
encoding = encoding == null ? UTF8 : encoding;
|
|
|
// 创建post方式请求对象
|