|
@@ -8,11 +8,19 @@ import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
|
|
+import org.apache.http.config.Registry;
|
|
|
+import org.apache.http.config.RegistryBuilder;
|
|
|
+import org.apache.http.conn.socket.ConnectionSocketFactory;
|
|
|
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|
|
+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.HttpClients;
|
|
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
import org.apache.http.message.BasicHeader;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.protocol.HTTP;
|
|
@@ -21,9 +29,15 @@ import org.apache.tomcat.util.http.fileupload.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import javax.net.ssl.TrustManager;
|
|
|
+import javax.net.ssl.X509TrustManager;
|
|
|
import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@@ -152,6 +166,8 @@ public class HttpUtil {
|
|
|
}
|
|
|
String str = EntityUtils.toString(new UrlEncodedFormEntity(pairs, StandardCharsets.UTF_8));//转换为键值对
|
|
|
HttpGet get = new HttpGet(url + SystemConstant.GET_UNKNOWN + str);
|
|
|
+ get.setHeader("Access-Control-Allow-Origin", SystemConstant.PATH_MATCH);
|
|
|
+ get.addHeader("Access-Control-Request-Method", SystemConstant.PATH_MATCH);
|
|
|
if (Objects.nonNull(secret)) {
|
|
|
get.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
|
|
|
}
|
|
@@ -189,6 +205,33 @@ public class HttpUtil {
|
|
|
return Objects.nonNull(ou) ? new String(ou.toByteArray(), StandardCharsets.UTF_8) : null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取响应信息
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getRespStringSsl(CloseableHttpClient client, HttpUriRequest request) throws IOException {
|
|
|
+ // 获取响应流
|
|
|
+ InputStream in = null;
|
|
|
+ ByteArrayOutputStream ou = null;
|
|
|
+ try {
|
|
|
+ in = getRespInputStreamSsl(client, request);
|
|
|
+ ou = new ByteArrayOutputStream();
|
|
|
+ IOUtils.copy(in, ou);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ } finally {
|
|
|
+ if (Objects.nonNull(in)) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(ou)) {
|
|
|
+ ou.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Objects.nonNull(ou) ? new String(ou.toByteArray(), StandardCharsets.UTF_8) : null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取输入流
|
|
|
*
|
|
@@ -220,6 +263,37 @@ public class HttpUtil {
|
|
|
return in;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取输入流
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static InputStream getRespInputStreamSsl(CloseableHttpClient client, HttpUriRequest request) {
|
|
|
+ // 获取响应对象
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = client.execute(request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ }
|
|
|
+ if (response == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 获取Entity对象
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ // 获取响应信息流
|
|
|
+ InputStream in = null;
|
|
|
+ if (entity != null) {
|
|
|
+ try {
|
|
|
+ in = entity.getContent();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return in;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 根据url下载文件,保存到filePath中
|
|
|
*
|
|
@@ -288,4 +362,82 @@ public class HttpUtil {
|
|
|
// 执行请求,获取响应
|
|
|
return getRespString(post);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * get 请求
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param params
|
|
|
+ * @param secret
|
|
|
+ * @param timestamp
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getSsl(String url, Map<String, Object> params, String secret, Long timestamp) throws IOException, NoSuchAlgorithmException, KeyManagementException {
|
|
|
+ SSLContext sslcontext = createIgnoreVerifySSL();
|
|
|
+ //设置协议http和https对应的处理socket链接工厂的对象
|
|
|
+ Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
+ .register("http", PlainConnectionSocketFactory.INSTANCE)
|
|
|
+ .register("https", new SSLConnectionSocketFactory(sslcontext))
|
|
|
+ .build();
|
|
|
+ PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
|
+ HttpClients.custom().setConnectionManager(connManager);
|
|
|
+
|
|
|
+ CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
|
|
|
+
|
|
|
+ // 构建请求参数
|
|
|
+ List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
|
|
|
+ if (params != null) {
|
|
|
+ for (String key : params.keySet()) {
|
|
|
+ pairs.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String str = EntityUtils.toString(new UrlEncodedFormEntity(pairs, StandardCharsets.UTF_8));//转换为键值对
|
|
|
+ HttpGet get = new HttpGet(url + SystemConstant.GET_UNKNOWN + str);
|
|
|
+// log.info("get url:{}", get.getURI());
|
|
|
+ get.setHeader("Access-Control-Allow-Origin", SystemConstant.PATH_MATCH);
|
|
|
+ get.addHeader("Access-Control-Request-Method", SystemConstant.PATH_MATCH);
|
|
|
+ if (Objects.nonNull(secret)) {
|
|
|
+ get.setHeader(SystemConstant.HEADER_AUTHORIZATION, secret);
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(timestamp)) {
|
|
|
+ get.setHeader(SystemConstant.HEADER_TIME, String.valueOf(timestamp));
|
|
|
+ }
|
|
|
+ // 执行请求,获取响应
|
|
|
+// return getRespString(get);
|
|
|
+ return getRespStringSsl(client, get);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 绕过验证
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @throws NoSuchAlgorithmException
|
|
|
+ * @throws KeyManagementException
|
|
|
+ */
|
|
|
+ public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
|
|
|
+ SSLContext sc = SSLContext.getInstance("SSLv3");
|
|
|
+
|
|
|
+ // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
|
|
+ X509TrustManager trustManager = new X509TrustManager() {
|
|
|
+ @Override
|
|
|
+ public void checkClientTrusted(
|
|
|
+ java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
|
|
|
+ String paramString) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkServerTrusted(
|
|
|
+ java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
|
|
|
+ String paramString) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ sc.init(null, new TrustManager[]{trustManager}, null);
|
|
|
+ return sc;
|
|
|
+ }
|
|
|
}
|