diff --git a/server/server-common/src/main/java/cn/torna/common/bean/HttpHelper.java b/server/server-common/src/main/java/cn/torna/common/bean/HttpHelper.java index 89b3c6326e4f5c60fa75f6b5a829dfdb9b21b933..cff351d2bf7baaa6c7ed287cc3e3e5ad36fbfc6c 100644 --- a/server/server-common/src/main/java/cn/torna/common/bean/HttpHelper.java +++ b/server/server-common/src/main/java/cn/torna/common/bean/HttpHelper.java @@ -20,6 +20,7 @@ import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode; @@ -29,6 +30,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HttpContext; +import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.util.EntityUtils; import java.io.Closeable; @@ -64,7 +66,17 @@ import java.util.Objects; */ public class HttpHelper { - private static final CloseableHttpClient CLOSEABLE_HTTP_CLIENT = HttpClients.createDefault(); + private static final CloseableHttpClient CLOSEABLE_HTTP_CLIENT; + + static { + try { + CLOSEABLE_HTTP_CLIENT = HttpClients.custom() + .setSSLSocketFactory(new SSLConnectionSocketFactory(new SSLContextBuilder() + .loadTrustMaterial(null, (chain, authType) -> true).build())).build(); + } catch (Exception e) { + throw new RuntimeException("init http client ssl error"); + } + } private RequestConfig requestConfig = RequestConfig .custom() @@ -148,7 +160,8 @@ public class HttpHelper { /** * 设置URL后面的参数,如:url?id=1&name=jim - * @param name 参数名 + * + * @param name 参数名 * @param value 参数值 * @return 返回HttpHelper */ @@ -199,6 +212,7 @@ public class HttpHelper { /** * 设置entity + * * @param entity entity * @return 返回HttpHelper */ diff --git a/server/server-common/src/test/java/cn/torna/common/HttpTest.java b/server/server-common/src/test/java/cn/torna/common/HttpTest.java index 745e561538467ad9d56f1b444457055e3e5d9d9a..8bcf20dc2256807501224bfc651a46693664aa1c 100644 --- a/server/server-common/src/test/java/cn/torna/common/HttpTest.java +++ b/server/server-common/src/test/java/cn/torna/common/HttpTest.java @@ -195,4 +195,18 @@ public class HttpTest { System.out.println(s2); } + /** + * 测试https + * @throws Exception + */ + @Test + public void httpsGet() throws Exception { + + String https = "https://localhost:8080/https/test"; + String s = HttpHelper.create().url(https) + .method("GET") + .execute().asString(); + System.out.println(s); + } + }