1414import org .apache .commons .codec .digest .DigestUtils ;
1515import org .apache .commons .lang3 .ArrayUtils ;
1616import org .apache .commons .lang3 .StringUtils ;
17+ import org .apache .http .Consts ;
18+ import org .apache .http .client .config .RequestConfig ;
1719import org .apache .http .client .methods .CloseableHttpResponse ;
1820import org .apache .http .client .methods .HttpPost ;
1921import org .apache .http .conn .ssl .DefaultHostnameVerifier ;
2729import javax .net .ssl .SSLContext ;
2830import java .io .File ;
2931import java .io .FileInputStream ;
32+ import java .io .IOException ;
3033import java .security .KeyStore ;
3134import java .util .*;
3235
@@ -199,7 +202,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
199202
200203 String url = PAY_BASE_URL + "/pay/orderquery" ;
201204
202- String responseContent = this .wxMpService . post (url , xstream .toXML (request ));
205+ String responseContent = this .executeRequest (url , xstream .toXML (request ));
203206 WxPayOrderQueryResult result = (WxPayOrderQueryResult ) xstream .fromXML (responseContent );
204207 result .composeCoupons (responseContent );
205208 if ("FAIL" .equals (result .getResultCode ())) {
@@ -233,7 +236,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
233236
234237 String url = PAY_BASE_URL + "/pay/closeorder" ;
235238
236- String responseContent = this .wxMpService . post (url , xstream .toXML (request ));
239+ String responseContent = this .executeRequest (url , xstream .toXML (request ));
237240 WxPayOrderCloseResult result = (WxPayOrderCloseResult ) xstream .fromXML (responseContent );
238241 if ("FAIL" .equals (result .getResultCode ())) {
239242 throw new WxErrorException (WxError .newBuilder ()
@@ -263,7 +266,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
263266
264267 String url = PAY_BASE_URL + "/pay/unifiedorder" ;
265268
266- String responseContent = this .wxMpService . post (url , xstream .toXML (request ));
269+ String responseContent = this .executeRequest (url , xstream .toXML (request ));
267270 WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult ) xstream
268271 .fromXML (responseContent );
269272 if ("FAIL" .equals (result .getResultCode ())) {
@@ -377,7 +380,27 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
377380 return result ;
378381 }
379382
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+
380402 private String executeRequestWithKeyFile ( String url , File keyFile , String requestStr , String mchId ) throws WxErrorException {
403+
381404 try (FileInputStream inputStream = new FileInputStream (keyFile )) {
382405 KeyStore keyStore = KeyStore .getInstance ("PKCS12" );
383406 keyStore .load (inputStream , mchId .toCharArray ());
@@ -386,13 +409,18 @@ private String executeRequestWithKeyFile( String url, File keyFile, String reque
386409 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslcontext , new String [] { "TLSv1" }, null ,
387410 new DefaultHostnameVerifier ());
388411
412+ HttpPost httpPost = new HttpPost (url );
413+ if (this .wxMpService .getHttpProxy () != null ) {
414+ httpPost .setConfig (RequestConfig .custom ().setProxy (this .wxMpService .getHttpProxy ()).build ());
415+ }
416+
389417 try (CloseableHttpClient httpclient = HttpClients .custom ().setSSLSocketFactory (sslsf ).build ()) {
390- HttpPost httpPost = new HttpPost (url );
391418 httpPost .setEntity (new StringEntity (new String (requestStr .getBytes ("UTF-8" ), "ISO-8859-1" )));
392-
393419 try (CloseableHttpResponse response = httpclient .execute (httpPost )) {
394- return EntityUtils .toString (response .getEntity ());
420+ return EntityUtils .toString (response .getEntity (), Consts . UTF_8 );
395421 }
422+ }finally {
423+ httpPost .releaseConnection ();
396424 }
397425 } catch (Exception e ) {
398426 throw new WxErrorException (WxError .newBuilder ().setErrorMsg (e .getMessage ()).build (), e );
0 commit comments