#715 修复http请求代理相关设置代码

This commit is contained in:
Binary Wang 2018-08-28 23:59:50 +08:00
parent 5f1290720e
commit 6f9371dfb7
3 changed files with 27 additions and 28 deletions

View File

@ -1,6 +1,7 @@
package me.chanjar.weixin.common.util.http.apache; package me.chanjar.weixin.common.util.http.apache;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.annotation.NotThreadSafe; import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
@ -236,9 +237,10 @@ public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder {
if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) { if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
// 使用代理服务器 需要用户认证的代理服务器 // 使用代理服务器 需要用户认证的代理服务器
CredentialsProvider provider = new BasicCredentialsProvider(); CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort) provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort),
, new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword)); new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword));
this.httpClientBuilder.setDefaultCredentialsProvider(provider); this.httpClientBuilder.setDefaultCredentialsProvider(provider);
this.httpClientBuilder.setProxy(new HttpHost(this.httpProxyHost, this.httpProxyPort));
} }
if (StringUtils.isNotBlank(this.userAgent)) { if (StringUtils.isNotBlank(this.userAgent)) {

View File

@ -1,6 +1,7 @@
package me.chanjar.weixin.common.util.http.apache; package me.chanjar.weixin.common.util.http.apache;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.annotation.NotThreadSafe; import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
@ -226,29 +227,26 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
.setConnectionManager(connectionManager) .setConnectionManager(connectionManager)
.setConnectionManagerShared(true) .setConnectionManagerShared(true)
.setSSLSocketFactory(this.buildSSLConnectionSocketFactory()) .setSSLSocketFactory(this.buildSSLConnectionSocketFactory())
.setDefaultRequestConfig( .setDefaultRequestConfig(RequestConfig.custom()
RequestConfig.custom() .setSocketTimeout(this.soTimeout)
.setSocketTimeout(this.soTimeout) .setConnectTimeout(this.connectionTimeout)
.setConnectTimeout(this.connectionTimeout) .setConnectionRequestTimeout(this.connectionRequestTimeout)
.setConnectionRequestTimeout(this.connectionRequestTimeout) .build()
.build() ).setRetryHandler(this.httpRequestRetryHandler);
)
.setRetryHandler(this.httpRequestRetryHandler);
if (StringUtils.isNotBlank(this.httpProxyHost) if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
&& StringUtils.isNotBlank(this.httpProxyUsername)) {
// 使用代理服务器 需要用户认证的代理服务器 // 使用代理服务器 需要用户认证的代理服务器
CredentialsProvider provider = new BasicCredentialsProvider(); CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials( provider.setCredentials(new AuthScope(this.httpProxyHost, this.httpProxyPort),
new AuthScope(this.httpProxyHost, this.httpProxyPort), new UsernamePasswordCredentials(this.httpProxyUsername, this.httpProxyPassword));
new UsernamePasswordCredentials(this.httpProxyUsername,
this.httpProxyPassword));
httpClientBuilder.setDefaultCredentialsProvider(provider); httpClientBuilder.setDefaultCredentialsProvider(provider);
httpClientBuilder.setProxy(new HttpHost(this.httpProxyHost, this.httpProxyPort));
} }
if (StringUtils.isNotBlank(this.userAgent)) { if (StringUtils.isNotBlank(this.userAgent)) {
httpClientBuilder.setUserAgent(this.userAgent); httpClientBuilder.setUserAgent(this.userAgent);
} }
this.closeableHttpClient = httpClientBuilder.build(); this.closeableHttpClient = httpClientBuilder.build();
prepared.set(true); prepared.set(true);
} }

View File

@ -1,10 +1,10 @@
package com.github.binarywang.wxpay.service.impl; package com.github.binarywang.wxpay.service.impl;
import java.io.UnsupportedEncodingException; import com.github.binarywang.wxpay.bean.WxPayApiData;
import java.nio.charset.StandardCharsets; import com.github.binarywang.wxpay.exception.WxPayException;
import javax.net.ssl.SSLContext; import jodd.util.Base64;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsProvider;
@ -20,9 +20,9 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import com.github.binarywang.wxpay.bean.WxPayApiData; import javax.net.ssl.SSLContext;
import com.github.binarywang.wxpay.exception.WxPayException; import java.io.UnsupportedEncodingException;
import jodd.util.Base64; import java.nio.charset.StandardCharsets;
/** /**
* <pre> * <pre>
@ -83,7 +83,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)); return new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
//cannot happen //cannot happen
this.log.error(e.getMessage(),e); this.log.error(e.getMessage(), e);
return null; return null;
} }
} }
@ -94,14 +94,13 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
this.initSSLContext(httpClientBuilder); this.initSSLContext(httpClientBuilder);
} }
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) && this.getConfig().getHttpProxyPort() > 0) {
&& this.getConfig().getHttpProxyPort() > 0) {
// 使用代理服务器 需要用户认证的代理服务器 // 使用代理服务器 需要用户认证的代理服务器
CredentialsProvider provider = new BasicCredentialsProvider(); CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials( provider.setCredentials(new AuthScope(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()),
new AuthScope(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()),
new UsernamePasswordCredentials(this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword())); new UsernamePasswordCredentials(this.getConfig().getHttpProxyUsername(), this.getConfig().getHttpProxyPassword()));
httpClientBuilder.setDefaultCredentialsProvider(provider); httpClientBuilder.setDefaultCredentialsProvider(provider);
httpClientBuilder.setProxy(new HttpHost(this.getConfig().getHttpProxyHost(), this.getConfig().getHttpProxyPort()));
} }
return httpClientBuilder; return httpClientBuilder;
} }