From 7553130cd5f906ec35c20db8a66834fdfd00e34c Mon Sep 17 00:00:00 2001 From: ecoolper Date: Tue, 18 Apr 2017 17:29:13 +0800 Subject: [PATCH 1/3] weixin-java-tools usage jodd-http --- .../wxpay/service/impl/WxPayServiceImplTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 93b0b8b552..b59bd1eb79 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 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 void testAuthcode2Openid() throws Exception { this.logger.info(result); } + } From 30cdad539c33b103cb9b79e5aaf7815e844b299d Mon Sep 17 00:00:00 2001 From: ecoolper Date: Tue, 18 Apr 2017 17:33:11 +0800 Subject: [PATCH 2/3] weixin-java-tools pay usage jodd-http --- .../wxpay/service/impl/WxPayServiceImpl.java | 34 +++++-------------- 1 file changed, 9 insertions(+), 25 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 c26d058392..ed609c2351 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.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. @@ -384,6 +376,7 @@ private String post(String url, String xmlParam) { /** * 由于暂时未找到使用jodd-http实现证书配置的办法,故而暂时使用httpclient + * ecoolper(20170418),修改为jodd-http方式 */ private String postWithKey(String url, String requestStr) throws WxErrorException { try { @@ -392,21 +385,12 @@ private String postWithKey(String url, String requestStr) throws WxErrorExceptio 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); From 0e6113b7658af6020b29e0b945b2a084d11ef740 Mon Sep 17 00:00:00 2001 From: ecoolper Date: Wed, 19 Apr 2017 10:39:06 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binarywang/wxpay/service/impl/WxPayServiceImpl.java | 5 ++--- 1 file changed, 2 insertions(+), 3 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 ed609c2351..2572f0911a 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 @@ -375,7 +375,6 @@ private String post(String url, String xmlParam) { } /** - * 由于暂时未找到使用jodd-http实现证书配置的办法,故而暂时使用httpclient * ecoolper(20170418),修改为jodd-http方式 */ private String postWithKey(String url, String requestStr) throws WxErrorException { @@ -385,9 +384,9 @@ private String postWithKey(String url, String requestStr) throws WxErrorExceptio sslContext = this.getConfig().initSSLContext(); } - HttpRequest request =HttpRequest.post(url).withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext)); + HttpRequest request = HttpRequest.post(url).withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext)); request.bodyText(requestStr); - HttpResponse response =request.send(); + HttpResponse response = request.send(); String result = response.bodyText(); this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, requestStr, result); return result;