Skip to content

Commit 6f9371d

Browse files
committed
#715 修复http请求代理相关设置代码
1 parent 5f12907 commit 6f9371d

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/ApacheHttpDnsClientBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.chanjar.weixin.common.util.http.apache;
22

33
import org.apache.commons.lang3.StringUtils;
4+
import org.apache.http.HttpHost;
45
import org.apache.http.annotation.NotThreadSafe;
56
import org.apache.http.auth.AuthScope;
67
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -236,9 +237,10 @@ private synchronized void prepare() {
236237
if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
237238
// 使用代理服务器 需要用户认证的代理服务器
238239
CredentialsProvider provider = new BasicCredentialsProvider();
239-
provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort)
240-
, new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword));
240+
provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort),
241+
new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword));
241242
this.httpClientBuilder.setDefaultCredentialsProvider(provider);
243+
this.httpClientBuilder.setProxy(new HttpHost(this.httpProxyHost, this.httpProxyPort));
242244
}
243245

244246
if (StringUtils.isNotBlank(this.userAgent)) {

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/DefaultApacheHttpClientBuilder.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.chanjar.weixin.common.util.http.apache;
22

33
import org.apache.commons.lang3.StringUtils;
4+
import org.apache.http.HttpHost;
45
import org.apache.http.annotation.NotThreadSafe;
56
import org.apache.http.auth.AuthScope;
67
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -226,29 +227,26 @@ private synchronized void prepare() {
226227
.setConnectionManager(connectionManager)
227228
.setConnectionManagerShared(true)
228229
.setSSLSocketFactory(this.buildSSLConnectionSocketFactory())
229-
.setDefaultRequestConfig(
230-
RequestConfig.custom()
231-
.setSocketTimeout(this.soTimeout)
232-
.setConnectTimeout(this.connectionTimeout)
233-
.setConnectionRequestTimeout(this.connectionRequestTimeout)
234-
.build()
235-
)
236-
.setRetryHandler(this.httpRequestRetryHandler);
237-
238-
if (StringUtils.isNotBlank(this.httpProxyHost)
239-
&& StringUtils.isNotBlank(this.httpProxyUsername)) {
230+
.setDefaultRequestConfig(RequestConfig.custom()
231+
.setSocketTimeout(this.soTimeout)
232+
.setConnectTimeout(this.connectionTimeout)
233+
.setConnectionRequestTimeout(this.connectionRequestTimeout)
234+
.build()
235+
).setRetryHandler(this.httpRequestRetryHandler);
236+
237+
if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
240238
// 使用代理服务器 需要用户认证的代理服务器
241239
CredentialsProvider provider = new BasicCredentialsProvider();
242-
provider.setCredentials(
243-
new AuthScope(this.httpProxyHost, this.httpProxyPort),
244-
new UsernamePasswordCredentials(this.httpProxyUsername,
245-
this.httpProxyPassword));
240+
provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort),
241+
new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword));
246242
httpClientBuilder.setDefaultCredentialsProvider(provider);
243+
httpClientBuilder.setProxy(new HttpHost(this.httpProxyHost, this.httpProxyPort));
247244
}
248245

249246
if (StringUtils.isNotBlank(this.userAgent)) {
250247
httpClientBuilder.setUserAgent(this.userAgent);
251248
}
249+
252250
this.closeableHttpClient = httpClientBuilder.build();
253251
prepared.set(true);
254252
}

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

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

3-
import java.io.UnsupportedEncodingException;
4-
import java.nio.charset.StandardCharsets;
5-
import javax.net.ssl.SSLContext;
6-
3+
import com.github.binarywang.wxpay.bean.WxPayApiData;
4+
import com.github.binarywang.wxpay.exception.WxPayException;
5+
import jodd.util.Base64;
76
import org.apache.commons.lang3.StringUtils;
7+
import org.apache.http.HttpHost;
88
import org.apache.http.auth.AuthScope;
99
import org.apache.http.auth.UsernamePasswordCredentials;
1010
import org.apache.http.client.CredentialsProvider;
@@ -20,9 +20,9 @@
2020
import org.apache.http.impl.client.HttpClients;
2121
import org.apache.http.util.EntityUtils;
2222

23-
import com.github.binarywang.wxpay.bean.WxPayApiData;
24-
import com.github.binarywang.wxpay.exception.WxPayException;
25-
import jodd.util.Base64;
23+
import javax.net.ssl.SSLContext;
24+
import java.io.UnsupportedEncodingException;
25+
import java.nio.charset.StandardCharsets;
2626

2727
/**
2828
* <pre>
@@ -83,7 +83,7 @@ private StringEntity createEntry(String requestStr) {
8383
return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
8484
} catch (UnsupportedEncodingException e) {
8585
//cannot happen
86-
this.log.error(e.getMessage(),e);
86+
this.log.error(e.getMessage(), e);
8787
return null;
8888
}
8989
}
@@ -94,14 +94,13 @@ private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayEx
9494
this.initSSLContext(httpClientBuilder);
9595
}
9696

97-
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost())
98-
&& this.getConfig().getHttpProxyPort() > 0) {
97+
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) {
9998
// 使用代理服务器 需要用户认证的代理服务器
10099
CredentialsProvider provider = new BasicCredentialsProvider();
101-
provider.setCredentials(
102-
new AuthScope(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()),
100+
provider.setCredentials(new AuthScope(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()),
103101
new UsernamePasswordCredentials(this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword()));
104102
httpClientBuilder.setDefaultCredentialsProvider(provider);
103+
httpClientBuilder.setProxy(new HttpHost(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()));
105104
}
106105
return httpClientBuilder;
107106
}

0 commit comments

Comments
 (0)