Skip to content

Commit 3067468

Browse files
committed
issue binarywang#29 http代理支持
1 parent d659e5b commit 3067468

16 files changed

Lines changed: 254 additions & 17 deletions

File tree

‎weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
99
import org.apache.commons.lang3.StringUtils;
1010
import org.apache.http.Header;
11+
import org.apache.http.HttpHost;
1112
import org.apache.http.client.ClientProtocolException;
13+
import org.apache.http.client.config.RequestConfig;
1214
import org.apache.http.client.methods.CloseableHttpResponse;
1315
import org.apache.http.client.methods.HttpGet;
1416
import org.apache.http.entity.ContentType;
17+
import org.apache.http.impl.client.CloseableHttpClient;
1518

1619
import java.io.File;
1720
import java.io.IOException;
@@ -27,7 +30,7 @@
2730
public class MediaDownloadRequestExecutor implements RequestExecutor<File, String> {
2831

2932
@Override
30-
public File execute(String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
33+
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
3134
if (queryParam != null) {
3235
if (uri.indexOf('?') == -1) {
3336
uri += '?';
@@ -36,6 +39,11 @@ public File execute(String uri, String queryParam) throws WxErrorException, Clie
3639
}
3740

3841
HttpGet httpGet = new HttpGet(uri);
42+
if (httpProxy != null) {
43+
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
44+
httpGet.setConfig(config);
45+
}
46+
3947
CloseableHttpResponse response = httpclient.execute(httpGet);
4048

4149
Header[] contentTypeHeader = response.getHeaders("Content-Type");

‎weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaUploadRequestExecutor.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55

66
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
77
import org.apache.http.HttpEntity;
8+
import org.apache.http.HttpHost;
89
import org.apache.http.client.ClientProtocolException;
10+
import org.apache.http.client.config.RequestConfig;
911
import org.apache.http.client.methods.CloseableHttpResponse;
1012
import org.apache.http.client.methods.HttpPost;
1113
import org.apache.http.entity.ContentType;
1214
import org.apache.http.entity.mime.MultipartEntityBuilder;
1315

1416
import me.chanjar.weixin.common.bean.result.WxError;
1517
import me.chanjar.weixin.common.exception.WxErrorException;
18+
import org.apache.http.impl.client.CloseableHttpClient;
1619

1720
/**
1821
* 上传媒体文件请求执行器,请求的参数是File, 返回的结果是String
@@ -22,8 +25,12 @@
2225
public class MediaUploadRequestExecutor implements RequestExecutor<WxMediaUploadResult, File> {
2326

2427
@Override
25-
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, ClientProtocolException, IOException {
28+
public WxMediaUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File file) throws WxErrorException, ClientProtocolException, IOException {
2629
HttpPost httpPost = new HttpPost(uri);
30+
if (httpProxy != null) {
31+
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
32+
httpPost.setConfig(config);
33+
}
2734
if (file != null) {
2835
HttpEntity entity = MultipartEntityBuilder
2936
.create()

‎weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/RequestExecutor.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44

5+
import org.apache.http.HttpHost;
56
import org.apache.http.client.ClientProtocolException;
67
import org.apache.http.impl.client.CloseableHttpClient;
78
import org.apache.http.impl.client.HttpClients;
@@ -17,8 +18,6 @@
1718
*/
1819
public interface RequestExecutor<T, E> {
1920

20-
public static final CloseableHttpClient httpclient = HttpClients.createDefault();
21+
public T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws WxErrorException, ClientProtocolException, IOException;
2122

22-
public T execute(String uri, E data) throws WxErrorException, ClientProtocolException, IOException;
23-
2423
}

‎weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import java.io.IOException;
44

5+
import org.apache.http.HttpHost;
56
import org.apache.http.client.ClientProtocolException;
7+
import org.apache.http.client.config.RequestConfig;
68
import org.apache.http.client.methods.CloseableHttpResponse;
79
import org.apache.http.client.methods.HttpGet;
810

911
import me.chanjar.weixin.common.bean.result.WxError;
1012
import me.chanjar.weixin.common.exception.WxErrorException;
13+
import org.apache.http.impl.client.CloseableHttpClient;
1114

1215
/**
1316
* 简单的GET请求执行器,请求的参数是String, 返回的结果也是String
@@ -17,14 +20,19 @@
1720
public class SimpleGetRequestExecutor implements RequestExecutor<String, String> {
1821

1922
@Override
20-
public String execute(String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
23+
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
2124
if (queryParam != null) {
2225
if (uri.indexOf('?') == -1) {
2326
uri += '?';
2427
}
2528
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
2629
}
2730
HttpGet httpGet = new HttpGet(uri);
31+
if (httpProxy != null) {
32+
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
33+
httpGet.setConfig(config);
34+
}
35+
2836
CloseableHttpResponse response = httpclient.execute(httpGet);
2937
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
3038
WxError error = WxError.fromJson(responseContent);

‎weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimplePostRequestExecutor.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
import me.chanjar.weixin.common.bean.result.WxError;
66
import me.chanjar.weixin.common.exception.WxErrorException;
77
import org.apache.http.Consts;
8+
import org.apache.http.HttpHost;
9+
import org.apache.http.auth.AuthScope;
10+
import org.apache.http.auth.UsernamePasswordCredentials;
811
import org.apache.http.client.ClientProtocolException;
12+
import org.apache.http.client.CredentialsProvider;
13+
import org.apache.http.client.config.RequestConfig;
914
import org.apache.http.client.methods.CloseableHttpResponse;
15+
import org.apache.http.client.methods.HttpGet;
1016
import org.apache.http.client.methods.HttpPost;
1117
import org.apache.http.entity.StringEntity;
18+
import org.apache.http.impl.client.BasicCredentialsProvider;
19+
import org.apache.http.impl.client.CloseableHttpClient;
20+
import org.apache.http.impl.client.HttpClients;
21+
import org.apache.http.util.EntityUtils;
1222

1323
/**
1424
* 简单的POST请求执行器,请求的参数是String, 返回的结果也是String
@@ -18,12 +28,18 @@
1828
public class SimplePostRequestExecutor implements RequestExecutor<String, String> {
1929

2030
@Override
21-
public String execute(String uri, String postEntity) throws WxErrorException, ClientProtocolException, IOException {
31+
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity) throws WxErrorException, ClientProtocolException, IOException {
2232
HttpPost httpPost = new HttpPost(uri);
33+
if (httpProxy != null) {
34+
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
35+
httpPost.setConfig(config);
36+
}
37+
2338
if (postEntity != null) {
2439
StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
2540
httpPost.setEntity(entity);
2641
}
42+
2743
CloseableHttpResponse response = httpclient.execute(httpPost);
2844
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
2945
WxError error = WxError.fromJson(responseContent);

‎weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpConfigStorage.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ public interface WxCpConfigStorage {
2727

2828
public int getExpiresIn();
2929

30+
public String getHttp_proxy_host();
31+
32+
public int getHttp_proxy_port();
33+
34+
public String getHttp_proxy_username();
35+
36+
public String getHttp_proxy_password();
37+
3038
}

‎weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpInMemoryConfigStorage.java‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
1818
protected String agentId;
1919
protected int expiresIn;
2020

21+
protected String http_proxy_host;
22+
protected int http_proxy_port;
23+
protected String http_proxy_username;
24+
protected String http_proxy_password;
25+
2126
public void updateAccessToken(WxAccessToken accessToken) {
2227
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
2328
}
@@ -83,16 +88,53 @@ public void setAgentId(String agentId) {
8388
this.agentId = agentId;
8489
}
8590

91+
public String getHttp_proxy_host() {
92+
return http_proxy_host;
93+
}
94+
95+
public void setHttp_proxy_host(String http_proxy_host) {
96+
this.http_proxy_host = http_proxy_host;
97+
}
98+
99+
public int getHttp_proxy_port() {
100+
return http_proxy_port;
101+
}
102+
103+
public void setHttp_proxy_port(int http_proxy_port) {
104+
this.http_proxy_port = http_proxy_port;
105+
}
106+
107+
public String getHttp_proxy_username() {
108+
return http_proxy_username;
109+
}
110+
111+
public void setHttp_proxy_username(String http_proxy_username) {
112+
this.http_proxy_username = http_proxy_username;
113+
}
114+
115+
public String getHttp_proxy_password() {
116+
return http_proxy_password;
117+
}
118+
119+
public void setHttp_proxy_password(String http_proxy_password) {
120+
this.http_proxy_password = http_proxy_password;
121+
}
122+
86123
@Override
87124
public String toString() {
88-
return "WxInMemoryCpConfigStorage{" +
89-
"appidOrCorpid='" + corpId + '\'' +
125+
return "WxCpInMemoryConfigStorage{" +
126+
"corpId='" + corpId + '\'' +
90127
", corpSecret='" + corpSecret + '\'' +
91128
", token='" + token + '\'' +
92129
", accessToken='" + accessToken + '\'' +
93130
", aesKey='" + aesKey + '\'' +
94131
", agentId='" + agentId + '\'' +
95132
", expiresIn=" + expiresIn +
133+
", http_proxy_host='" + http_proxy_host + '\'' +
134+
", http_proxy_port=" + http_proxy_port +
135+
", http_proxy_username='" + http_proxy_username + '\'' +
136+
", http_proxy_password='" + http_proxy_password + '\'' +
96137
'}';
97138
}
139+
98140
}

‎weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
import me.chanjar.weixin.common.util.crypto.SHA1;
2121
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
2222
import org.apache.commons.lang3.StringUtils;
23+
import org.apache.http.HttpHost;
24+
import org.apache.http.auth.AuthScope;
25+
import org.apache.http.auth.UsernamePasswordCredentials;
2326
import org.apache.http.client.ClientProtocolException;
27+
import org.apache.http.client.CredentialsProvider;
2428
import org.apache.http.client.methods.CloseableHttpResponse;
2529
import org.apache.http.client.methods.HttpGet;
30+
import org.apache.http.impl.client.BasicCredentialsProvider;
2631
import org.apache.http.impl.client.BasicResponseHandler;
2732
import org.apache.http.impl.client.CloseableHttpClient;
2833
import org.apache.http.impl.client.HttpClients;
@@ -51,12 +56,14 @@ public class WxCpServiceImpl implements WxCpService {
5156
*/
5257
protected static final AtomicBoolean GLOBAL_ACCESS_TOKEN_REFRESH_FLAG = new AtomicBoolean(false);
5358

54-
protected static final CloseableHttpClient httpclient = HttpClients.createDefault();
55-
5659
protected WxCpConfigStorage wxCpConfigStorage;
5760

5861
protected final ThreadLocal<Integer> retryTimes = new ThreadLocal<Integer>();
5962

63+
protected CloseableHttpClient httpClient;
64+
65+
protected HttpHost httpProxy;
66+
6067
public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
6168
try {
6269
return SHA1.gen(wxCpConfigStorage.getToken(), timestamp, nonce, data).equals(msgSignature);
@@ -78,6 +85,7 @@ public void accessTokenRefresh() throws WxErrorException {
7885
+ "&corpsecret=" + wxCpConfigStorage.getCorpSecret();
7986
try {
8087
HttpGet httpGet = new HttpGet(url);
88+
CloseableHttpClient httpclient = getHttpclient();
8189
CloseableHttpResponse response = httpclient.execute(httpGet);
8290
String resultContent = new BasicResponseHandler().handleResponse(response);
8391
WxError error = WxError.fromJson(resultContent);
@@ -335,7 +343,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
335343
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
336344

337345
try {
338-
return executor.execute(uriWithAccessToken, data);
346+
return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data);
339347
} catch (WxErrorException e) {
340348
WxError error = e.getError();
341349
/*
@@ -379,8 +387,38 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
379387
}
380388
}
381389

390+
protected CloseableHttpClient getHttpclient() {
391+
return httpClient;
392+
}
393+
382394
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
383395
this.wxCpConfigStorage = wxConfigProvider;
396+
397+
String http_proxy_host = wxCpConfigStorage.getHttp_proxy_host();
398+
int http_proxy_port = wxCpConfigStorage.getHttp_proxy_port();
399+
String http_proxy_username = wxCpConfigStorage.getHttp_proxy_username();
400+
String http_proxy_password = wxCpConfigStorage.getHttp_proxy_password();
401+
402+
if(StringUtils.isNotBlank(http_proxy_host)) {
403+
// 使用代理服务器
404+
if(StringUtils.isNotBlank(http_proxy_username)) {
405+
// 需要用户认证的代理服务器
406+
CredentialsProvider credsProvider = new BasicCredentialsProvider();
407+
credsProvider.setCredentials(
408+
new AuthScope(http_proxy_host, http_proxy_port),
409+
new UsernamePasswordCredentials(http_proxy_username, http_proxy_password));
410+
httpClient = HttpClients
411+
.custom()
412+
.setDefaultCredentialsProvider(credsProvider)
413+
.build();
414+
} else {
415+
// 无需用户认证的代理服务器
416+
httpClient = HttpClients.createDefault();
417+
}
418+
httpProxy = new HttpHost(http_proxy_host, http_proxy_port);
419+
} else {
420+
httpClient = HttpClients.createDefault();
421+
}
384422
}
385423

386424
}

‎weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMediaAPITest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8+
import me.chanjar.weixin.common.api.WxConsts;
89
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
910
import org.testng.Assert;
1011
import org.testng.annotations.DataProvider;

‎weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageAPITest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import me.chanjar.weixin.common.api.WxConsts;
34
import me.chanjar.weixin.cp.bean.WxCpMessage;
45
import org.testng.annotations.Guice;
56
import org.testng.annotations.Test;

0 commit comments

Comments
 (0)