From ec4bf2687c1d51ea13836beaac7d01a8955ed2b2 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 18 Jun 2017 12:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E8=AF=B7=E6=B1=82=E6=9F=90=E4=BA=9B=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=9C=A8=E6=9F=90=E4=BA=9B=E6=83=85=E5=86=B5=E4=B8=8B=E4=BC=9A?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E4=B9=B1=E7=A0=81=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=20#225?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wxpay/service/impl/WxPayServiceImpl.java | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 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 a7228774..c28697a8 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 @@ -474,18 +474,9 @@ public class WxPayServiceImpl implements WxPayService { } HttpRequest request = HttpRequest.post(url).body(requestString); - HttpResponse response = request.send(); - String responseString = response.bodyText(); - - if (needTransferEncoding) { - try { - responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } + String responseString = this.getResponseString(request.send()); - this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, xmlParam, responseString); + this.log.info("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, xmlParam, responseString); return responseString; } @@ -499,16 +490,35 @@ public class WxPayServiceImpl implements WxPayService { sslContext = this.getConfig().initSSLContext(); } - 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; + HttpRequest request = HttpRequest + .post(url) + .withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext)) + .bodyText(requestStr); + + String responseString = this.getResponseString(request.send()); + + this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, requestStr, responseString); + return responseString; } catch (Exception e) { - this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage()); + this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", url, requestStr, e.getMessage()); throw new WxPayException(e.getMessage()); } } + private String getResponseString(HttpResponse response) { + this.log.debug("【微信服务器响应头信息】:\n{}", response.toString(false)); + + String responseString = response.bodyText(); + + if (StringUtils.isBlank(response.charset())) { + try { + responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + return responseString; + } + + }