diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java index b51f28e9..d512cbe7 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java @@ -39,6 +39,10 @@ public class WxPayConfig { private SSLContext sslContext; private String keyPath; private boolean useSandboxEnv = false; + private String httpProxyHost; + private Integer httpProxyPort; + private String httpProxyUsername; + private String httpProxyPassword; public String getKeyPath() { return keyPath; @@ -227,4 +231,36 @@ public class WxPayConfig { public void setHttpTimeout(int httpTimeout) { this.httpTimeout = httpTimeout; } + + public String getHttpProxyHost() { + return httpProxyHost; + } + + public void setHttpProxyHost(String httpProxyHost) { + this.httpProxyHost = httpProxyHost; + } + + public Integer getHttpProxyPort() { + return httpProxyPort; + } + + public void setHttpProxyPort(Integer httpProxyPort) { + this.httpProxyPort = httpProxyPort; + } + + public String getHttpProxyUsername() { + return httpProxyUsername; + } + + public void setHttpProxyUsername(String httpProxyUsername) { + this.httpProxyUsername = httpProxyUsername; + } + + public String getHttpProxyPassword() { + return httpProxyPassword; + } + + public void setHttpProxyPassword(String httpProxyPassword) { + this.httpProxyPassword = httpProxyPassword; + } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java index c5da982c..09c414f4 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java @@ -1,12 +1,17 @@ package com.github.binarywang.wxpay.service.impl; import com.github.binarywang.wxpay.exception.WxPayException; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.DefaultHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; @@ -41,12 +46,23 @@ public class WxPayServiceApacheHttpImpl extends WxPayServiceAbstractImpl { } HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(RequestConfig.custom() .setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout()) .setConnectTimeout(this.getConfig().getHttpConnectionTimeout()) .setSocketTimeout(this.getConfig().getHttpTimeout()) .build()); + if (StringUtils.isNotBlank(this.config.getHttpProxyHost()) + && StringUtils.isNotBlank(this.config.getHttpProxyUsername())) { + // 使用代理服务器 需要用户认证的代理服务器 + CredentialsProvider provider = new BasicCredentialsProvider(); + provider.setCredentials( + new AuthScope(this.config.getHttpProxyHost(), this.config.getHttpProxyPort()), + new UsernamePasswordCredentials(this.config.getHttpProxyUsername(), this.config.getHttpProxyPassword())); + httpClientBuilder.setDefaultCredentialsProvider(provider); + } + try (CloseableHttpClient httpclient = httpClientBuilder.build()) { httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1))); try (CloseableHttpResponse response = httpclient.execute(httpPost)) {