httpclient,EntityUtils.toString(httpEntity)多次调用错误;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter(“name”);
String idCard = request.getParameter(“idCard”);
String timestamp = request.getParameter(“timestamp”);
String sign = request.getParameter(“sign”);
// 加密验证
Map<String, Object> mapSign = verifySign(timestamp, sign);
if (mapSign.get("statusCode").toString().equals("0")) {
logger.info("[VerificationIDCard]verifySign false, timestamp=" + timestamp + ", sign=" + sign);
this.getResult(request, response, "0", "验签失败");
return;
}
logger.info("[VerificationIDCard][验证身份开始]");
Map<String, Object> ret = new HashMap<String, Object>();
String host = "https://idenauthen.market.alicloudapi.com";
String path = "/idenAuthentication";
String method = "POST";
String appcode = "1f0f652aefb948aeb8c8e135c88a45b3";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
Map<String, String> bodys = new HashMap<String, String>();
System.out.println("身份证号:"+idCard);
if(null != idCard && !"".equals(idCard) && isIDNumber(idCard)) {
logger.info("[VerificationIDCard][身份证号不为空]");
bodys.put("idNo", idCard);
}else {
logger.info("[VerificationIDCard][身份证号为空]");
ret.put("code", "0");
ret.put("message", "身份证号异常!");
ConstantUtil.reJSON(ret.toString(), request, response);
return;
}
logger.info("姓名:"+name);
if(null != name && !"".equals(name)) {
logger.info("[VerificationIDCard][姓名不为空]");
bodys.put("name", name);
}else {
logger.info("[VerificationIDCard][姓名不空]");
ret.put("code", "0");
ret.put("message", "姓名异常!");
ConstantUtil.reJSON(ret.toString(), request, response);
return;
}
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
logger.info("请求开始:");
logger.info("host:"+host+",path:"+path+",method:"+method+",headers:"+headers+",querys:"+querys+",bodys:"+bodys);
HttpResponse data = AliyunHttpUtils.doPost(host, path, method, headers, querys, bodys);
//获取response的body
String entity = EntityUtils.toString(data.getEntity());
llogger.info("[VerificationIDCard] 返回参数:"+EntityUtils.toString(data.getEntity()));
ConstantUtil.reJSON(entity, request, response);
return;
} catch (Exception e) {
e.printStackTrace();
ret.put("code", "0");
ret.put("message", "服务器错误!");
ConstantUtil.reJSON(ret.toString(), request, response);
return;
}
}
原因是如下特别指出我的脚本中以下2个输出,这个输出中调用了2次 EntityUtils.toString(httpEntity) ,而根据httpclient的官方说明中,EntityUtils.toString(httpEntity) 这个被调用一次后就会自动销毁,而我调用了2次所有就报错了
String entity = EntityUtils.toString(data.getEntity());
llogger.info("[VerificationIDCard] 返回参数:"+EntityUtils.toString(data.getEntity()));
于是把这2个输出脚本改为如下即可,只要调用一次就好
String entity = EntityUtils.toString(data.getEntity());
logger.info("[VerificationIDCard] 返回参数:"+entity);