|
1 | 1 | package com.github.binarywang.wxpay.service.impl; |
2 | 2 |
|
3 | 3 | import com.github.binarywang.wxpay.exception.WxPayException; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.apache.http.auth.AuthScope; |
| 6 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 7 | +import org.apache.http.client.CredentialsProvider; |
4 | 8 | import org.apache.http.client.config.RequestConfig; |
5 | 9 | import org.apache.http.client.methods.CloseableHttpResponse; |
6 | 10 | import org.apache.http.client.methods.HttpPost; |
7 | 11 | import org.apache.http.conn.ssl.DefaultHostnameVerifier; |
8 | 12 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
9 | 13 | import org.apache.http.entity.StringEntity; |
| 14 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
10 | 15 | import org.apache.http.impl.client.CloseableHttpClient; |
11 | 16 | import org.apache.http.impl.client.HttpClientBuilder; |
12 | 17 | import org.apache.http.impl.client.HttpClients; |
@@ -41,12 +46,23 @@ protected String post(String url, String requestStr, boolean useKey) throws WxPa |
41 | 46 | } |
42 | 47 |
|
43 | 48 | HttpPost httpPost = new HttpPost(url); |
| 49 | + |
44 | 50 | httpPost.setConfig(RequestConfig.custom() |
45 | 51 | .setConnectionRequestTimeout(this.getConfig().getHttpConnectionTimeout()) |
46 | 52 | .setConnectTimeout(this.getConfig().getHttpConnectionTimeout()) |
47 | 53 | .setSocketTimeout(this.getConfig().getHttpTimeout()) |
48 | 54 | .build()); |
49 | 55 |
|
| 56 | + if (StringUtils.isNotBlank(this.config.getHttpProxyHost()) |
| 57 | + && StringUtils.isNotBlank(this.config.getHttpProxyUsername())) { |
| 58 | + // 使用代理服务器 需要用户认证的代理服务器 |
| 59 | + CredentialsProvider provider = new BasicCredentialsProvider(); |
| 60 | + provider.setCredentials( |
| 61 | + new AuthScope(this.config.getHttpProxyHost(), this.config.getHttpProxyPort()), |
| 62 | + new UsernamePasswordCredentials(this.config.getHttpProxyUsername(), this.config.getHttpProxyPassword())); |
| 63 | + httpClientBuilder.setDefaultCredentialsProvider(provider); |
| 64 | + } |
| 65 | + |
50 | 66 | try (CloseableHttpClient httpclient = httpClientBuilder.build()) { |
51 | 67 | httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1))); |
52 | 68 | try (CloseableHttpResponse response = httpclient.execute(httpPost)) { |
|
0 commit comments