Skip to content

Commit a8d443e

Browse files
crskypbinarywang
authored andcommitted
pay模块使用jodd-http方式,已支持证书方式 (binarywang#189)
weixin-java-tools pay use jodd-http
1 parent 1efec6b commit a8d443e

File tree

2 files changed

+14
-29
lines changed

2 files changed

+14
-29
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,19 @@
99
import com.google.common.collect.Maps;
1010
import jodd.http.HttpRequest;
1111
import jodd.http.HttpResponse;
12+
import jodd.http.net.SSLSocketHttpConnectionProvider;
1213
import me.chanjar.weixin.common.bean.result.WxError;
1314
import me.chanjar.weixin.common.exception.WxErrorException;
1415
import org.apache.commons.lang3.CharEncoding;
1516
import 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;
2517
import org.slf4j.Logger;
2618
import org.slf4j.LoggerFactory;
2719

28-
import javax.net.ssl.SSLContext;
2920
import java.io.File;
3021
import java.io.UnsupportedEncodingException;
3122
import java.util.HashMap;
3223
import 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);

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.binarywang.wxpay.service.impl;
22

3+
import static org.testng.Assert.*;
4+
35
import com.github.binarywang.utils.qrcode.QrcodeUtils;
46
import com.github.binarywang.wxpay.bean.request.*;
57
import com.github.binarywang.wxpay.bean.result.*;
@@ -10,15 +12,14 @@
1012
import me.chanjar.weixin.common.exception.WxErrorException;
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
13-
import org.testng.annotations.*;
15+
import org.testng.annotations.Guice;
16+
import org.testng.annotations.Test;
1417

1518
import java.io.File;
1619
import java.nio.file.Files;
1720
import java.nio.file.Path;
1821
import java.util.Map;
1922

20-
import static org.testng.Assert.*;
21-
2223
/**
2324
* 测试支付相关接口
2425
* Created by Binary Wang on 2016/7/28.
@@ -264,4 +265,5 @@ public void testAuthcode2Openid() throws Exception {
264265
this.logger.info(result);
265266
}
266267

268+
267269
}

0 commit comments

Comments
 (0)