Просмотр исходного кода

#307 微信支付模块中增加http proxy设置的支持

master
Binary Wang 7 лет назад
committed by Binary Wang
Родитель
Сommit
66c786d4e6
2 измененных файлов: 52 добавлений и 0 удалений
  1. +36
    -0
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java
  2. +16
    -0
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java

+ 36
- 0
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;
}
}

+ 16
- 0
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)) {


Загрузка…
Отмена
Сохранить