14
14
import org .apache .commons .codec .digest .DigestUtils ;
15
15
import org .apache .commons .lang3 .ArrayUtils ;
16
16
import org .apache .commons .lang3 .StringUtils ;
17
+ import org .apache .http .Consts ;
18
+ import org .apache .http .client .config .RequestConfig ;
17
19
import org .apache .http .client .methods .CloseableHttpResponse ;
18
20
import org .apache .http .client .methods .HttpPost ;
19
21
import org .apache .http .conn .ssl .DefaultHostnameVerifier ;
27
29
import javax .net .ssl .SSLContext ;
28
30
import java .io .File ;
29
31
import java .io .FileInputStream ;
32
+ import java .io .IOException ;
30
33
import java .security .KeyStore ;
31
34
import java .util .*;
32
35
@@ -199,7 +202,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
199
202
200
203
String url = PAY_BASE_URL + "/pay/orderquery" ;
201
204
202
- String responseContent = this .wxMpService . post (url , xstream .toXML (request ));
205
+ String responseContent = this .executeRequest (url , xstream .toXML (request ));
203
206
WxPayOrderQueryResult result = (WxPayOrderQueryResult ) xstream .fromXML (responseContent );
204
207
result .composeCoupons (responseContent );
205
208
if ("FAIL" .equals (result .getResultCode ())) {
@@ -233,7 +236,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
233
236
234
237
String url = PAY_BASE_URL + "/pay/closeorder" ;
235
238
236
- String responseContent = this .wxMpService . post (url , xstream .toXML (request ));
239
+ String responseContent = this .executeRequest (url , xstream .toXML (request ));
237
240
WxPayOrderCloseResult result = (WxPayOrderCloseResult ) xstream .fromXML (responseContent );
238
241
if ("FAIL" .equals (result .getResultCode ())) {
239
242
throw new WxErrorException (WxError .newBuilder ()
@@ -263,7 +266,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
263
266
264
267
String url = PAY_BASE_URL + "/pay/unifiedorder" ;
265
268
266
- String responseContent = this .wxMpService . post (url , xstream .toXML (request ));
269
+ String responseContent = this .executeRequest (url , xstream .toXML (request ));
267
270
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult ) xstream
268
271
.fromXML (responseContent );
269
272
if ("FAIL" .equals (result .getResultCode ())) {
@@ -377,7 +380,27 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
377
380
return result ;
378
381
}
379
382
383
+ private String executeRequest ( String url , String requestStr ) throws WxErrorException {
384
+ HttpPost httpPost = new HttpPost (url );
385
+ if (this .wxMpService .getHttpProxy () != null ) {
386
+ httpPost .setConfig (RequestConfig .custom ().setProxy (this .wxMpService .getHttpProxy ()).build ());
387
+ }
388
+
389
+ try (CloseableHttpClient httpclient = HttpClients .custom ().build ()) {
390
+ httpPost .setEntity (new StringEntity (new String (requestStr .getBytes ("UTF-8" ), "ISO-8859-1" )));
391
+
392
+ try (CloseableHttpResponse response = httpclient .execute (httpPost )) {
393
+ return EntityUtils .toString (response .getEntity (), Consts .UTF_8 );
394
+ }
395
+ } catch (IOException e ) {
396
+ throw new WxErrorException (WxError .newBuilder ().setErrorMsg (e .getMessage ()).build (), e );
397
+ }finally {
398
+ httpPost .releaseConnection ();
399
+ }
400
+ }
401
+
380
402
private String executeRequestWithKeyFile ( String url , File keyFile , String requestStr , String mchId ) throws WxErrorException {
403
+
381
404
try (FileInputStream inputStream = new FileInputStream (keyFile )) {
382
405
KeyStore keyStore = KeyStore .getInstance ("PKCS12" );
383
406
keyStore .load (inputStream , mchId .toCharArray ());
@@ -386,13 +409,18 @@ private String executeRequestWithKeyFile( String url, File keyFile, String reque
386
409
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslcontext , new String [] { "TLSv1" }, null ,
387
410
new DefaultHostnameVerifier ());
388
411
412
+ HttpPost httpPost = new HttpPost (url );
413
+ if (this .wxMpService .getHttpProxy () != null ) {
414
+ httpPost .setConfig (RequestConfig .custom ().setProxy (this .wxMpService .getHttpProxy ()).build ());
415
+ }
416
+
389
417
try (CloseableHttpClient httpclient = HttpClients .custom ().setSSLSocketFactory (sslsf ).build ()) {
390
- HttpPost httpPost = new HttpPost (url );
391
418
httpPost .setEntity (new StringEntity (new String (requestStr .getBytes ("UTF-8" ), "ISO-8859-1" )));
392
-
393
419
try (CloseableHttpResponse response = httpclient .execute (httpPost )) {
394
- return EntityUtils .toString (response .getEntity ());
420
+ return EntityUtils .toString (response .getEntity (), Consts . UTF_8 );
395
421
}
422
+ }finally {
423
+ httpPost .releaseConnection ();
396
424
}
397
425
} catch (Exception e ) {
398
426
throw new WxErrorException (WxError .newBuilder ().setErrorMsg (e .getMessage ()).build (), e );
0 commit comments