java.io.IOException: Attempted read from closed stream.

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值