如果出现异常的情况下,将请求的数据打印出来。

This commit is contained in:
fengyapeng 2017-01-18 15:38:12 +08:00
parent 7e243f3a30
commit 5c251da562

View File

@ -132,13 +132,11 @@ public class WeixinRequestExecutor {
if (logger.isEnabled(InternalLogLevel.DEBUG)) {
logger.debug("weixin request >> " + request.getMethod() + " "
+ request.getURI().toString());
}
if(request.getEntity() instanceof StringEntity){
logger.debug("weixin request body >> " + ((StringEntity) request.getEntity()).getContentString());
printHttpRequest(request,InternalLogLevel.DEBUG);
}
HttpResponse httpResponse = httpClient.execute(request);
WeixinResponse response = new WeixinResponse(httpResponse);
handleResponse(response);
handleResponse(request,response);
return response;
} catch (HttpClientException e) {
throw new WeixinException(e);
@ -170,7 +168,7 @@ public class WeixinRequestExecutor {
* 微信请求响应
* @throws WeixinException
*/
protected void handleResponse(WeixinResponse response)
protected void handleResponse(HttpRequest request,WeixinResponse response)
throws WeixinException {
boolean hasStreamMimeType = hasStreamMimeType(response);
if (logger.isEnabled(InternalLogLevel.DEBUG)) {
@ -187,6 +185,7 @@ public class WeixinRequestExecutor {
ApiResult result = response.getAsResult();
if (!SUCCESS_CODE.contains(String.format(",%s,", result.getReturnCode()
.toLowerCase()))) {
printHttpRequest(request,InternalLogLevel.WARN);
throw new WeixinException(result.getReturnCode(),
result.getReturnMsg(),WeixinErrorUtil2.getText(result.getReturnCode()));
}
@ -196,6 +195,7 @@ public class WeixinRequestExecutor {
XmlResult.class, response);
if (!SUCCESS_CODE.contains(String.format(",%s,", xmlResult
.getResultCode().toLowerCase()))) {
printHttpRequest(request,InternalLogLevel.WARN);
throw new WeixinException(xmlResult.getErrCode(),
xmlResult.getErrCodeDes(),WeixinErrorUtil2.getText(xmlResult.getErrCode()));
}
@ -205,6 +205,18 @@ public class WeixinRequestExecutor {
}
}
private void printHttpRequest(HttpRequest request,InternalLogLevel level){
if(request.getEntity() instanceof StringEntity){
if(level.equals(InternalLogLevel.DEBUG)) {
logger.debug("weixin request body >> " + ((StringEntity) request.getEntity()).getContentString());
}else if(level.equals(InternalLogLevel.INFO)){
logger.info("weixin request body >> " + ((StringEntity) request.getEntity()).getContentString());
}else if(level.equals(InternalLogLevel.WARN)){
logger.warn("weixin request body >> " + ((StringEntity) request.getEntity()).getContentString());
}
}
}
public HttpClient getExecuteClient() {
return httpClient;
}