Skip to content

Commit 82e2371

Browse files
007gzsbinarywang
authored andcommitted
binarywang#644 WxOpenConfigStorage增加setApacheHttpClientBuilder()方法,方便用户修改http请求相关参数
1 parent 0cbd354 commit 82e2371

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.chanjar.weixin.open.api;
22

33
import cn.binarywang.wx.miniapp.config.WxMaConfig;
4+
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
45
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
56
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
67
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
@@ -46,6 +47,8 @@ public interface WxOpenConfigStorage {
4647

4748
String getHttpProxyPassword();
4849

50+
ApacheHttpClientBuilder getApacheHttpClientBuilder();
51+
4952
WxMpConfigStorage getWxMpConfigStorage(String appId);
5053

5154
WxMaConfig getWxMaConfig(String appId);

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
3434
private int httpProxyPort;
3535
private String httpProxyUsername;
3636
private String httpProxyPassword;
37+
private ApacheHttpClientBuilder apacheHttpClientBuilder;
3738

3839
private Map<String, Token> authorizerRefreshTokens = new Hashtable<>();
3940
private Map<String, Token> authorizerAccessTokens = new Hashtable<>();
@@ -146,6 +147,15 @@ public void setHttpProxyPassword(String httpProxyPassword) {
146147
this.httpProxyPassword = httpProxyPassword;
147148
}
148149

150+
@Override
151+
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
152+
return apacheHttpClientBuilder;
153+
}
154+
155+
public ApacheHttpClientBuilder setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
156+
return this.apacheHttpClientBuilder = apacheHttpClientBuilder;
157+
}
158+
149159
@Override
150160
public WxMpConfigStorage getWxMpConfigStorage(String appId) {
151161
return new WxOpenInnerConfigStorage(this, appId);
@@ -448,7 +458,7 @@ public File getTmpDirFile() {
448458

449459
@Override
450460
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
451-
return null;
461+
return wxOpenConfigStorage.getApacheHttpClientBuilder();
452462
}
453463

454464

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenServiceApacheHttpClientImpl.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import me.chanjar.weixin.common.util.http.HttpType;
55
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
66
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
7+
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
78
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
9+
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
810
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
911
import org.apache.http.HttpHost;
1012
import org.apache.http.impl.client.CloseableHttpClient;
@@ -21,14 +23,22 @@ public class WxOpenServiceApacheHttpClientImpl extends WxOpenServiceAbstractImpl
2123
@Override
2224
public void initHttp() {
2325
WxOpenConfigStorage configStorage = this.getWxOpenConfigStorage();
24-
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
25-
this.httpProxy = new HttpHost(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort());
26+
ApacheHttpClientBuilder apacheHttpClientBuilder = configStorage.getApacheHttpClientBuilder();
27+
if (null == apacheHttpClientBuilder) {
28+
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
2629
}
27-
this.httpClient = DefaultApacheHttpClientBuilder.get()
28-
.httpProxyHost(configStorage.getHttpProxyHost())
30+
31+
apacheHttpClientBuilder.httpProxyHost(configStorage.getHttpProxyHost())
2932
.httpProxyPort(configStorage.getHttpProxyPort())
3033
.httpProxyUsername(configStorage.getHttpProxyUsername())
31-
.httpProxyPassword(configStorage.getHttpProxyPassword()).build();
34+
.httpProxyPassword(configStorage.getHttpProxyPassword());
35+
36+
if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {
37+
this.httpProxy = new HttpHost(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort());
38+
}
39+
40+
this.httpClient = apacheHttpClientBuilder.build();
41+
3242
}
3343

3444
@Override

0 commit comments

Comments
 (0)