Skip to content

Commit 66c786d

Browse files
committed
#307 微信支付模块中增加http proxy设置的支持
1 parent 09ed365 commit 66c786d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class WxPayConfig {
3939
private SSLContext sslContext;
4040
private String keyPath;
4141
private boolean useSandboxEnv = false;
42+
private String httpProxyHost;
43+
private Integer httpProxyPort;
44+
private String httpProxyUsername;
45+
private String httpProxyPassword;
4246

4347
public String getKeyPath() {
4448
return keyPath;
@@ -227,4 +231,36 @@ public int getHttpTimeout() {
227231
public void setHttpTimeout(int httpTimeout) {
228232
this.httpTimeout = httpTimeout;
229233
}
234+
235+
public String getHttpProxyHost() {
236+
return httpProxyHost;
237+
}
238+
239+
public void setHttpProxyHost(String httpProxyHost) {
240+
this.httpProxyHost = httpProxyHost;
241+
}
242+
243+
public Integer getHttpProxyPort() {
244+
return httpProxyPort;
245+
}
246+
247+
public void setHttpProxyPort(Integer httpProxyPort) {
248+
this.httpProxyPort = httpProxyPort;
249+
}
250+
251+
public String getHttpProxyUsername() {
252+
return httpProxyUsername;
253+
}
254+
255+
public void setHttpProxyUsername(String httpProxyUsername) {
256+
this.httpProxyUsername = httpProxyUsername;
257+
}
258+
259+
public String getHttpProxyPassword() {
260+
return httpProxyPassword;
261+
}
262+
263+
public void setHttpProxyPassword(String httpProxyPassword) {
264+
this.httpProxyPassword = httpProxyPassword;
265+
}
230266
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.github.binarywang.wxpay.service.impl;
22

33
import com.github.binarywang.wxpay.exception.WxPayException;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.apache.http.auth.AuthScope;
6+
import org.apache.http.auth.UsernamePasswordCredentials;
7+
import org.apache.http.client.CredentialsProvider;
48
import org.apache.http.client.config.RequestConfig;
59
import org.apache.http.client.methods.CloseableHttpResponse;
610
import org.apache.http.client.methods.HttpPost;
711
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
812
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
913
import org.apache.http.entity.StringEntity;
14+
import org.apache.http.impl.client.BasicCredentialsProvider;
1015
import org.apache.http.impl.client.CloseableHttpClient;
1116
import org.apache.http.impl.client.HttpClientBuilder;
1217
import org.apache.http.impl.client.HttpClients;
@@ -41,12 +46,23 @@ protected String post(String url, String requestStr, boolean useKey) throws WxPa
4146
}
4247

4348
HttpPost httpPost = new HttpPost(url);
49+
4450
httpPost.setConfig(RequestConfig.custom()
4551
.setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout())
4652
.setConnectTimeout(this.getConfig().getHttpConnectionTimeout())
4753
.setSocketTimeout(this.getConfig().getHttpTimeout())
4854
.build());
4955

56+
if (StringUtils.isNotBlank(this.config.getHttpProxyHost())
57+
&& StringUtils.isNotBlank(this.config.getHttpProxyUsername())) {
58+
// 使用代理服务器 需要用户认证的代理服务器
59+
CredentialsProvider provider = new BasicCredentialsProvider();
60+
provider.setCredentials(
61+
new AuthScope(this.config.getHttpProxyHost(), this.config.getHttpProxyPort()),
62+
new UsernamePasswordCredentials(this.config.getHttpProxyUsername(), this.config.getHttpProxyPassword()));
63+
httpClientBuilder.setDefaultCredentialsProvider(provider);
64+
}
65+
5066
try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
5167
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)));
5268
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {

0 commit comments

Comments
 (0)