mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	#307 微信支付模块中增加http proxy设置的支持
This commit is contained in:
		| @ -39,6 +39,10 @@ public class WxPayConfig { | |||||||
|   private SSLContext sslContext; |   private SSLContext sslContext; | ||||||
|   private String keyPath; |   private String keyPath; | ||||||
|   private boolean useSandboxEnv = false; |   private boolean useSandboxEnv = false; | ||||||
|  |   private String httpProxyHost; | ||||||
|  |   private Integer httpProxyPort; | ||||||
|  |   private String httpProxyUsername; | ||||||
|  |   private String httpProxyPassword; | ||||||
|  |  | ||||||
|   public String getKeyPath() { |   public String getKeyPath() { | ||||||
|     return keyPath; |     return keyPath; | ||||||
| @ -227,4 +231,36 @@ public class WxPayConfig { | |||||||
|   public void setHttpTimeout(int httpTimeout) { |   public void setHttpTimeout(int httpTimeout) { | ||||||
|     this.httpTimeout = 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; | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,12 +1,17 @@ | |||||||
| package com.github.binarywang.wxpay.service.impl; | package com.github.binarywang.wxpay.service.impl; | ||||||
|  |  | ||||||
| import com.github.binarywang.wxpay.exception.WxPayException; | 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.config.RequestConfig; | ||||||
| import org.apache.http.client.methods.CloseableHttpResponse; | import org.apache.http.client.methods.CloseableHttpResponse; | ||||||
| import org.apache.http.client.methods.HttpPost; | import org.apache.http.client.methods.HttpPost; | ||||||
| import org.apache.http.conn.ssl.DefaultHostnameVerifier; | import org.apache.http.conn.ssl.DefaultHostnameVerifier; | ||||||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||||||
| import org.apache.http.entity.StringEntity; | 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.CloseableHttpClient; | ||||||
| import org.apache.http.impl.client.HttpClientBuilder; | import org.apache.http.impl.client.HttpClientBuilder; | ||||||
| import org.apache.http.impl.client.HttpClients; | import org.apache.http.impl.client.HttpClients; | ||||||
| @ -41,12 +46,23 @@ public class WxPayServiceApacheHttpImpl extends WxPayServiceAbstractImpl { | |||||||
|       } |       } | ||||||
|  |  | ||||||
|       HttpPost httpPost = new HttpPost(url); |       HttpPost httpPost = new HttpPost(url); | ||||||
|  |  | ||||||
|       httpPost.setConfig(RequestConfig.custom() |       httpPost.setConfig(RequestConfig.custom() | ||||||
|         .setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout()) |         .setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout()) | ||||||
|         .setConnectTimeout(this.getConfig().getHttpConnectionTimeout()) |         .setConnectTimeout(this.getConfig().getHttpConnectionTimeout()) | ||||||
|         .setSocketTimeout(this.getConfig().getHttpTimeout()) |         .setSocketTimeout(this.getConfig().getHttpTimeout()) | ||||||
|         .build()); |         .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()) { |       try (CloseableHttpClient httpclient = httpClientBuilder.build()) { | ||||||
|         httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1))); |         httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1))); | ||||||
|         try (CloseableHttpResponse response = httpclient.execute(httpPost)) { |         try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang