99import com .google .common .collect .Maps ;
1010import jodd .http .HttpRequest ;
1111import jodd .http .HttpResponse ;
12+ import jodd .http .net .SSLSocketHttpConnectionProvider ;
1213import me .chanjar .weixin .common .bean .result .WxError ;
1314import me .chanjar .weixin .common .exception .WxErrorException ;
1415import org .apache .commons .lang3 .CharEncoding ;
1516import org .apache .commons .lang3 .StringUtils ;
16- import org .apache .http .Consts ;
17- import org .apache .http .client .methods .CloseableHttpResponse ;
18- import org .apache .http .client .methods .HttpPost ;
19- import org .apache .http .conn .ssl .DefaultHostnameVerifier ;
20- import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
21- import org .apache .http .entity .StringEntity ;
22- import org .apache .http .impl .client .CloseableHttpClient ;
23- import org .apache .http .impl .client .HttpClients ;
24- import org .apache .http .util .EntityUtils ;
2517import org .slf4j .Logger ;
2618import org .slf4j .LoggerFactory ;
2719
28- import javax .net .ssl .SSLContext ;
2920import java .io .File ;
3021import java .io .UnsupportedEncodingException ;
3122import java .util .HashMap ;
3223import java .util .Map ;
24+ import javax .net .ssl .SSLContext ;
3325
3426/**
3527 * Created by Binary Wang on 2016/7/28.
@@ -403,7 +395,7 @@ private String post(String url, String xmlParam) {
403395 }
404396
405397 /**
406- * 由于暂时未找到使用jodd-http实现证书配置的办法,故而暂时使用httpclient
398+ * ecoolper(20170418),修改为jodd-http方式
407399 */
408400 private String postWithKey (String url , String requestStr ) throws WxErrorException {
409401 try {
@@ -412,21 +404,12 @@ private String postWithKey(String url, String requestStr) throws WxErrorExceptio
412404 sslContext = this .getConfig ().initSSLContext ();
413405 }
414406
415- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslContext ,
416- new String []{"TLSv1" }, null , new DefaultHostnameVerifier ());
417-
418- HttpPost httpPost = new HttpPost (url );
419-
420- try (CloseableHttpClient httpclient = HttpClients .custom ().setSSLSocketFactory (sslsf ).build ()) {
421- httpPost .setEntity (new StringEntity (new String (requestStr .getBytes (CharEncoding .UTF_8 ), CharEncoding .ISO_8859_1 )));
422- try (CloseableHttpResponse response = httpclient .execute (httpPost )) {
423- String result = EntityUtils .toString (response .getEntity (), Consts .UTF_8 );
424- this .log .debug ("\n [URL]: {}\n [PARAMS]: {}\n [RESPONSE]: {}" , url , requestStr , result );
425- return result ;
426- }
427- } finally {
428- httpPost .releaseConnection ();
429- }
407+ HttpRequest request = HttpRequest .post (url ).withConnectionProvider (new SSLSocketHttpConnectionProvider (sslContext ));
408+ request .bodyText (requestStr );
409+ HttpResponse response = request .send ();
410+ String result = response .bodyText ();
411+ this .log .debug ("\n [URL]: {}\n [PARAMS]: {}\n [RESPONSE]: {}" , url , requestStr , result );
412+ return result ;
430413 } catch (Exception e ) {
431414 this .log .error ("\n [URL]: {}\n [PARAMS]: {}\n [EXCEPTION]: {}" , url , requestStr , e .getMessage ());
432415 throw new WxErrorException (WxError .newBuilder ().setErrorCode (-1 ).setErrorMsg (e .getMessage ()).build (), e );
0 commit comments