From 41f8b49d614c6210b4b38bcc5c217c5fe46daddb Mon Sep 17 00:00:00 2001
From: liyi
Date: Tue, 29 Oct 2019 17:46:59 +0800
Subject: [PATCH] # 215
---
changelog.txt | 4 ++
pom.xml | 2 +-
src/main/java/weixin/popular/Version.java | 2 +-
.../popular/client/HttpClientFactory.java | 38 ++++++++++++++-----
4 files changed, 34 insertions(+), 12 deletions(-)
diff --git a/changelog.txt b/changelog.txt
index 25f1f34f..72940402 100644
--- a/changelog.txt
+++ b/changelog.txt
@@ -2,6 +2,10 @@ WEIXIN-POPULAR CHANGELOG
===========================
https://github.com/liyiorg/weixin-popular
+Changes in version 2.8.29 (2019-10-29)
+-------------------------------------
+* #215 添加HttpClient 代理配置
+
Changes in version 2.8.28 (2019-07-26)
-------------------------------------
* #199 刷卡支付 授权码查询OPENID接口 添加返回字段sub_openid
diff --git a/pom.xml b/pom.xml
index 03cc773c..681137a3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
com.github.liyiorg
weixin-popular
- 2.8.28
+ 2.8.29
weixin-popular
The weixin-popular is a JAVA SDK for weixin. Weixin web url is https://mp.weixin.qq.com.
diff --git a/src/main/java/weixin/popular/Version.java b/src/main/java/weixin/popular/Version.java
index f4daf61b..dfec5b7c 100644
--- a/src/main/java/weixin/popular/Version.java
+++ b/src/main/java/weixin/popular/Version.java
@@ -2,5 +2,5 @@
public interface Version {
- String VERSION = "2.8.28";
+ String VERSION = "2.8.29";
}
diff --git a/src/main/java/weixin/popular/client/HttpClientFactory.java b/src/main/java/weixin/popular/client/HttpClientFactory.java
index e6b7f52d..a38b52ce 100644
--- a/src/main/java/weixin/popular/client/HttpClientFactory.java
+++ b/src/main/java/weixin/popular/client/HttpClientFactory.java
@@ -13,6 +13,7 @@
import javax.net.ssl.SSLException;
import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.protocol.HttpClientContext;
@@ -38,9 +39,20 @@ public class HttpClientFactory{
private static final String[] supportedProtocols = new String[]{"TLSv1"};
+ private static HttpHost proxy;
+
public static CloseableHttpClient createHttpClient() {
return createHttpClient(100,10,5000,2);
}
+
+ /**
+ * 设置代理
+ * @since 2.8.29
+ * @param proxy 代理
+ */
+ public static void setProxy(HttpHost proxy){
+ HttpClientFactory.proxy = proxy;
+ }
/**
*
@@ -59,11 +71,14 @@ public static CloseableHttpClient createHttpClient(int maxTotal,int maxPerRoute,
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(timeout).build();
poolingHttpClientConnectionManager.setDefaultSocketConfig(socketConfig);
- return HttpClientBuilder.create()
- .setConnectionManager(poolingHttpClientConnectionManager)
- .setSSLSocketFactory(sf)
- .setRetryHandler(new HttpRequestRetryHandlerImpl(retryExecutionCount))
- .build();
+ HttpClientBuilder builder = HttpClientBuilder.create();
+ if(proxy != null){
+ builder.setProxy(proxy);
+ }
+ return builder.setConnectionManager(poolingHttpClientConnectionManager)
+ .setSSLSocketFactory(sf)
+ .setRetryHandler(new HttpRequestRetryHandlerImpl(retryExecutionCount))
+ .build();
} catch (KeyManagementException e) {
logger.error("", e);
} catch (NoSuchAlgorithmException e) {
@@ -99,11 +114,14 @@ public static CloseableHttpClient createKeyMaterialHttpClient(KeyStore keystore,
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslContext,supportedProtocols,
null,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(timeout).build();
- return HttpClientBuilder.create()
- .setDefaultSocketConfig(socketConfig)
- .setSSLSocketFactory(sf)
- .setRetryHandler(new HttpRequestRetryHandlerImpl(retryExecutionCount))
- .build();
+ HttpClientBuilder builder = HttpClientBuilder.create();
+ if(proxy != null){
+ builder.setProxy(proxy);
+ }
+ return builder.setDefaultSocketConfig(socketConfig)
+ .setSSLSocketFactory(sf)
+ .setRetryHandler(new HttpRequestRetryHandlerImpl(retryExecutionCount))
+ .build();
} catch (KeyManagementException e) {
logger.error("", e);
} catch (NoSuchAlgorithmException e) {