|
@@ -1,8 +1,9 @@ |
|
|
package com.github.binarywang.wxpay.service.impl; |
|
|
package com.github.binarywang.wxpay.service.impl; |
|
|
|
|
|
|
|
|
import com.github.binarywang.wxpay.bean.WxPayApiData; |
|
|
|
|
|
import com.github.binarywang.wxpay.exception.WxPayException; |
|
|
|
|
|
import jodd.util.Base64; |
|
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
import javax.net.ssl.SSLContext; |
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.http.auth.AuthScope; |
|
|
import org.apache.http.auth.AuthScope; |
|
|
import org.apache.http.auth.UsernamePasswordCredentials; |
|
|
import org.apache.http.auth.UsernamePasswordCredentials; |
|
@@ -19,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 javax.net.ssl.SSLContext; |
|
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
|
import com.github.binarywang.wxpay.bean.WxPayApiData; |
|
|
|
|
|
import com.github.binarywang.wxpay.exception.WxPayException; |
|
|
|
|
|
import jodd.util.Base64; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* <pre> |
|
|
* <pre> |
|
@@ -37,8 +38,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { |
|
|
try { |
|
|
try { |
|
|
HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey); |
|
|
HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey); |
|
|
HttpPost httpPost = this.createHttpPost(url, requestStr); |
|
|
HttpPost httpPost = this.createHttpPost(url, requestStr); |
|
|
try (CloseableHttpClient httpclient = httpClientBuilder.build()) { |
|
|
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httpPost)) { |
|
|
|
|
|
|
|
|
try (CloseableHttpClient httpClient = httpClientBuilder.build()) { |
|
|
|
|
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { |
|
|
final byte[] bytes = EntityUtils.toByteArray(response.getEntity()); |
|
|
final byte[] bytes = EntityUtils.toByteArray(response.getEntity()); |
|
|
final String responseData = Base64.encodeToString(bytes); |
|
|
final String responseData = Base64.encodeToString(bytes); |
|
|
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseData); |
|
|
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseData); |
|
@@ -60,8 +61,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { |
|
|
try { |
|
|
try { |
|
|
HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey); |
|
|
HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey); |
|
|
HttpPost httpPost = this.createHttpPost(url, requestStr); |
|
|
HttpPost httpPost = this.createHttpPost(url, requestStr); |
|
|
try (CloseableHttpClient httpclient = httpClientBuilder.build()) { |
|
|
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httpPost)) { |
|
|
|
|
|
|
|
|
try (CloseableHttpClient httpClient = httpClientBuilder.build()) { |
|
|
|
|
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { |
|
|
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
|
|
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
|
|
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString); |
|
|
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString); |
|
|
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null)); |
|
|
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null)); |
|
@@ -90,7 +91,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { |
|
|
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException { |
|
|
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException { |
|
|
HttpClientBuilder httpClientBuilder = HttpClients.custom(); |
|
|
HttpClientBuilder httpClientBuilder = HttpClients.custom(); |
|
|
if (useKey) { |
|
|
if (useKey) { |
|
|
this.setKey(httpClientBuilder); |
|
|
|
|
|
|
|
|
this.initSSLContext(httpClientBuilder); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) |
|
|
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost()) |
|
@@ -118,15 +119,15 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { |
|
|
return httpPost; |
|
|
return httpPost; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void setKey(HttpClientBuilder httpClientBuilder) throws WxPayException { |
|
|
|
|
|
|
|
|
private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException { |
|
|
SSLContext sslContext = this.getConfig().getSslContext(); |
|
|
SSLContext sslContext = this.getConfig().getSslContext(); |
|
|
if (null == sslContext) { |
|
|
if (null == sslContext) { |
|
|
sslContext = this.getConfig().initSSLContext(); |
|
|
sslContext = this.getConfig().initSSLContext(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, |
|
|
|
|
|
|
|
|
SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, |
|
|
new String[]{"TLSv1"}, null, new DefaultHostnameVerifier()); |
|
|
new String[]{"TLSv1"}, null, new DefaultHostnameVerifier()); |
|
|
httpClientBuilder.setSSLSocketFactory(sslsf); |
|
|
|
|
|
|
|
|
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |