Skip to content

Commit ec4bf26

Browse files
committed
修复微信支付请求某些接口在某些情况下会出现乱码的情况 #225
1 parent 87687b3 commit ec4bf26

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -474,18 +474,9 @@ private String post(String url, String xmlParam, boolean needTransferEncoding) {
474474
}
475475

476476
HttpRequest request = HttpRequest.post(url).body(requestString);
477-
HttpResponse response = request.send();
478-
String responseString = response.bodyText();
479-
480-
if (needTransferEncoding) {
481-
try {
482-
responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8);
483-
} catch (UnsupportedEncodingException e) {
484-
e.printStackTrace();
485-
}
486-
}
477+
String responseString = this.getResponseString(request.send());
487478

488-
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, xmlParam, responseString);
479+
this.log.info("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, xmlParam, responseString);
489480
return responseString;
490481
}
491482

@@ -499,16 +490,35 @@ private String postWithKey(String url, String requestStr) throws WxPayException
499490
sslContext = this.getConfig().initSSLContext();
500491
}
501492

502-
HttpRequest request = HttpRequest.post(url).withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext));
503-
request.bodyText(requestStr);
504-
HttpResponse response = request.send();
505-
String result = response.bodyText();
506-
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, requestStr, result);
507-
return result;
493+
HttpRequest request = HttpRequest
494+
.post(url)
495+
.withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext))
496+
.bodyText(requestStr);
497+
498+
String responseString = this.getResponseString(request.send());
499+
500+
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, requestStr, responseString);
501+
return responseString;
508502
} catch (Exception e) {
509-
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
503+
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
510504
throw new WxPayException(e.getMessage());
511505
}
512506
}
513507

508+
private String getResponseString(HttpResponse response) {
509+
this.log.debug("【微信服务器响应头信息】:\n{}", response.toString(false));
510+
511+
String responseString = response.bodyText();
512+
513+
if (StringUtils.isBlank(response.charset())) {
514+
try {
515+
responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8);
516+
} catch (UnsupportedEncodingException e) {
517+
e.printStackTrace();
518+
}
519+
}
520+
return responseString;
521+
}
522+
523+
514524
}

0 commit comments

Comments
 (0)