修复微信支付请求某些接口在某些情况下会出现乱码的情况 #225

This commit is contained in:
Binary Wang 2017-06-18 12:52:31 +08:00
parent 87687b3369
commit ec4bf2687c

View File

@ -474,18 +474,9 @@ public class WxPayServiceImpl implements WxPayService {
} }
HttpRequest request = HttpRequest.post(url).body(requestString); HttpRequest request = HttpRequest.post(url).body(requestString);
HttpResponse response = request.send(); String responseString = this.getResponseString(request.send());
String responseString = response.bodyText();
if (needTransferEncoding) { this.log.info("\n【请求地址】: {}\n【请求参数】{}\n【响应数据】{}", url, xmlParam, responseString);
try {
responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, xmlParam, responseString);
return responseString; return responseString;
} }
@ -499,16 +490,35 @@ public class WxPayServiceImpl implements WxPayService {
sslContext = this.getConfig().initSSLContext(); sslContext = this.getConfig().initSSLContext();
} }
HttpRequest request = HttpRequest.post(url).withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext)); HttpRequest request = HttpRequest
request.bodyText(requestStr); .post(url)
HttpResponse response = request.send(); .withConnectionProvider(new SSLSocketHttpConnectionProvider(sslContext))
String result = response.bodyText(); .bodyText(requestStr);
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, requestStr, result);
return result; String responseString = this.getResponseString(request.send());
this.log.debug("\n【请求地址】: {}\n【请求参数】{}\n【响应数据】{}", url, requestStr, responseString);
return responseString;
} catch (Exception e) { } 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()); 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;
}
} }