From a8d443e13be71c791f447828ac8f31e171beed12 Mon Sep 17 00:00:00 2001 From: crskyp Date: Wed, 19 Apr 2017 11:25:50 +0800 Subject: [PATCH] =?UTF-8?q?pay=E6=A8=A1=E5=9D=97=E4=BD=BF=E7=94=A8jodd-htt?= =?UTF-8?q?p=E6=96=B9=E5=BC=8F=EF=BC=8C=E5=B7=B2=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=AF=81=E4=B9=A6=E6=96=B9=E5=BC=8F=20(#189)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit weixin-java-tools pay use jodd-http --- .../wxpay/service/impl/WxPayServiceImpl.java | 35 +++++-------------- .../service/impl/WxPayServiceImplTest.java | 8 +++-- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java index 0e4dd28d..1a949bc0 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java @@ -9,27 +9,19 @@ import com.github.binarywang.wxpay.util.SignUtils; import com.google.common.collect.Maps; import jodd.http.HttpRequest; import jodd.http.HttpResponse; +import jodd.http.net.SSLSocketHttpConnectionProvider; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringUtils; -import org.apache.http.Consts; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.conn.ssl.DefaultHostnameVerifier; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.net.ssl.SSLContext; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; +import javax.net.ssl.SSLContext; /** * Created by Binary Wang on 2016/7/28. @@ -403,7 +395,7 @@ public class WxPayServiceImpl implements WxPayService { } /** - * 由于暂时未找到使用jodd-http实现证书配置的办法,故而暂时使用httpclient + * ecoolper(20170418),修改为jodd-http方式 */ private String postWithKey(String url, String requestStr) throws WxErrorException { try { @@ -412,21 +404,12 @@ public class WxPayServiceImpl implements WxPayService { sslContext = this.getConfig().initSSLContext(); } - SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, - new String[]{"TLSv1"}, null, new DefaultHostnameVerifier()); - - HttpPost httpPost = new HttpPost(url); - - try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) { - httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(CharEncoding.UTF_8), CharEncoding.ISO_8859_1))); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { - String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8); - this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, requestStr, result); - return result; - } - } finally { - httpPost.releaseConnection(); - } + HttpRequest request = HttpRequest.post(url).withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext)); + request.bodyText(requestStr); + HttpResponse response = request.send(); + String result = response.bodyText(); + this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, requestStr, result); + return result; } catch (Exception e) { this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage()); throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e); diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java index 93b0b8b5..b59bd1eb 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java @@ -1,5 +1,7 @@ package com.github.binarywang.wxpay.service.impl; +import static org.testng.Assert.*; + import com.github.binarywang.utils.qrcode.QrcodeUtils; import com.github.binarywang.wxpay.bean.request.*; import com.github.binarywang.wxpay.bean.result.*; @@ -10,15 +12,14 @@ import com.google.inject.Inject; import me.chanjar.weixin.common.exception.WxErrorException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.testng.annotations.*; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; -import static org.testng.Assert.*; - /** * 测试支付相关接口 * Created by Binary Wang on 2016/7/28. @@ -264,4 +265,5 @@ public class WxPayServiceImplTest { this.logger.info(result); } + }