1
1
package com .github .binarywang .wxpay .service .impl ;
2
2
3
- import com .github .binarywang .wxpay .bean .WxPayApiData ;
4
- import com .github .binarywang .wxpay .exception .WxPayException ;
5
- import jodd .util .Base64 ;
3
+ import java .io .UnsupportedEncodingException ;
4
+ import java .nio .charset .StandardCharsets ;
5
+ import javax .net .ssl .SSLContext ;
6
+
6
7
import org .apache .commons .lang3 .StringUtils ;
7
8
import org .apache .http .auth .AuthScope ;
8
9
import org .apache .http .auth .UsernamePasswordCredentials ;
19
20
import org .apache .http .impl .client .HttpClients ;
20
21
import org .apache .http .util .EntityUtils ;
21
22
22
- import javax . net . ssl . SSLContext ;
23
- import java . io . UnsupportedEncodingException ;
24
- import java . nio . charset . StandardCharsets ;
23
+ import com . github . binarywang . wxpay . bean . WxPayApiData ;
24
+ import com . github . binarywang . wxpay . exception . WxPayException ;
25
+ import jodd . util . Base64 ;
25
26
26
27
/**
27
28
* <pre>
@@ -37,8 +38,8 @@ public byte[] postForBytes(String url, String requestStr, boolean useKey) throws
37
38
try {
38
39
HttpClientBuilder httpClientBuilder = createHttpClientBuilder (useKey );
39
40
HttpPost httpPost = this .createHttpPost (url , requestStr );
40
- try (CloseableHttpClient httpclient = httpClientBuilder .build ()) {
41
- try (CloseableHttpResponse response = httpclient .execute (httpPost )) {
41
+ try (CloseableHttpClient httpClient = httpClientBuilder .build ()) {
42
+ try (CloseableHttpResponse response = httpClient .execute (httpPost )) {
42
43
final byte [] bytes = EntityUtils .toByteArray (response .getEntity ());
43
44
final String responseData = Base64 .encodeToString (bytes );
44
45
this .log .info ("\n 【请求地址】:{}\n 【请求数据】:{}\n 【响应数据(Base64编码后)】:{}" , url , requestStr , responseData );
@@ -60,8 +61,8 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
60
61
try {
61
62
HttpClientBuilder httpClientBuilder = this .createHttpClientBuilder (useKey );
62
63
HttpPost httpPost = this .createHttpPost (url , requestStr );
63
- try (CloseableHttpClient httpclient = httpClientBuilder .build ()) {
64
- try (CloseableHttpResponse response = httpclient .execute (httpPost )) {
64
+ try (CloseableHttpClient httpClient = httpClientBuilder .build ()) {
65
+ try (CloseableHttpResponse response = httpClient .execute (httpPost )) {
65
66
String responseString = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
66
67
this .log .info ("\n 【请求地址】:{}\n 【请求数据】:{}\n 【响应数据】:{}" , url , requestStr , responseString );
67
68
wxApiData .set (new WxPayApiData (url , requestStr , responseString , null ));
@@ -90,7 +91,7 @@ private StringEntity createEntry(String requestStr) {
90
91
private HttpClientBuilder createHttpClientBuilder (boolean useKey ) throws WxPayException {
91
92
HttpClientBuilder httpClientBuilder = HttpClients .custom ();
92
93
if (useKey ) {
93
- this .setKey (httpClientBuilder );
94
+ this .initSSLContext (httpClientBuilder );
94
95
}
95
96
96
97
if (StringUtils .isNotBlank (this .getConfig ().getHttpProxyHost ())
@@ -118,15 +119,15 @@ private HttpPost createHttpPost(String url, String requestStr) {
118
119
return httpPost ;
119
120
}
120
121
121
- private void setKey (HttpClientBuilder httpClientBuilder ) throws WxPayException {
122
+ private void initSSLContext (HttpClientBuilder httpClientBuilder ) throws WxPayException {
122
123
SSLContext sslContext = this .getConfig ().getSslContext ();
123
124
if (null == sslContext ) {
124
125
sslContext = this .getConfig ().initSSLContext ();
125
126
}
126
127
127
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslContext ,
128
+ SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory (sslContext ,
128
129
new String []{"TLSv1" }, null , new DefaultHostnameVerifier ());
129
- httpClientBuilder .setSSLSocketFactory (sslsf );
130
+ httpClientBuilder .setSSLSocketFactory (connectionSocketFactory );
130
131
}
131
132
132
133
}
0 commit comments