11package com .github .binarywang .wxpay .service .impl ;
22
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+
67import org .apache .commons .lang3 .StringUtils ;
78import org .apache .http .auth .AuthScope ;
89import org .apache .http .auth .UsernamePasswordCredentials ;
1920import org .apache .http .impl .client .HttpClients ;
2021import org .apache .http .util .EntityUtils ;
2122
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 ;
2526
2627/**
2728 * <pre>
@@ -37,8 +38,8 @@ public byte[] postForBytes(String url, String requestStr, boolean useKey) throws
3738 try {
3839 HttpClientBuilder httpClientBuilder = createHttpClientBuilder (useKey );
3940 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 )) {
4243 final byte [] bytes = EntityUtils .toByteArray (response .getEntity ());
4344 final String responseData = Base64 .encodeToString (bytes );
4445 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
6061 try {
6162 HttpClientBuilder httpClientBuilder = this .createHttpClientBuilder (useKey );
6263 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 )) {
6566 String responseString = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
6667 this .log .info ("\n 【请求地址】:{}\n 【请求数据】:{}\n 【响应数据】:{}" , url , requestStr , responseString );
6768 wxApiData .set (new WxPayApiData (url , requestStr , responseString , null ));
@@ -90,7 +91,7 @@ private StringEntity createEntry(String requestStr) {
9091 private HttpClientBuilder createHttpClientBuilder (boolean useKey ) throws WxPayException {
9192 HttpClientBuilder httpClientBuilder = HttpClients .custom ();
9293 if (useKey ) {
93- this .setKey (httpClientBuilder );
94+ this .initSSLContext (httpClientBuilder );
9495 }
9596
9697 if (StringUtils .isNotBlank (this .getConfig ().getHttpProxyHost ())
@@ -118,15 +119,15 @@ private HttpPost createHttpPost(String url, String requestStr) {
118119 return httpPost ;
119120 }
120121
121- private void setKey (HttpClientBuilder httpClientBuilder ) throws WxPayException {
122+ private void initSSLContext (HttpClientBuilder httpClientBuilder ) throws WxPayException {
122123 SSLContext sslContext = this .getConfig ().getSslContext ();
123124 if (null == sslContext ) {
124125 sslContext = this .getConfig ().initSSLContext ();
125126 }
126127
127- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslContext ,
128+ SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory (sslContext ,
128129 new String []{"TLSv1" }, null , new DefaultHostnameVerifier ());
129- httpClientBuilder .setSSLSocketFactory (sslsf );
130+ httpClientBuilder .setSSLSocketFactory (connectionSocketFactory );
130131 }
131132
132133}
0 commit comments