前言:
代码如下,执行的时候提示“java.io.IOException: Attempted read from closed stream.”
public static JSONObject post(String url,StringBuffer params,String token ){
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url + "?" + params);
httpPost.setHeader("Content-Type", "application/json, text/plain, */*");
httpPost.setHeader("Authorization",token);
// 响应模型
CloseableHttpResponse response = null;
try {
// send the Post Request
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
String jsonString = EntityUtils.toString(responseEntity);
resBody = JSONObject.fromObject(jsonString);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//release resource
release(httpClient,response);
}
return resBody;
}
原因
response.getEntity()所得到的流是不可重复读取的,所得的实体只能读取一次,读取一次后,流就关闭了。EntityUtils.toString(responseEntity)被调用一次后就会自动销毁,而我调用了2次,所以就报错了。
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
String jsonString = EntityUtils.toString(responseEntity);
解决方法
把这2个输出脚本改为如下即可,只要调用一次就好:
String jsonString = EntityUtils.toString(responseEntity);
System.out.println("响应内容为:" + jsonString);
java.io.IOException: Attempted read from closed stream的更多相关文章
-
java.io.IOException: Attempted read from closed stream解决
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...
-
使用HttpClient出现java.io.IOException: Attempted read from closed stream
问题描述: 使用httpClient时候,出现java.io.IOException: Attempted read from closed stream. 原始代码: public static S ...
-
java.io.IOException: Attempted read from closed stream. 异常,解决
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...
-
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决
原因是EntityUtils.toString(HttpEntity)方法被使用了多次.所以每个方法内只能使用一次.
-
java.io.IOException: Stream closed
今天在做SSH项目的时候,出现了这个错误.百思不得其解,网上的答案都不能解决我的问题-.. 后来,一气之下就重新写,写了之后发现在JSP遍历集合的时候出错了. <s:iterator value ...
-
java.io.IOException: Stream closed解决办法
1.出现这个bug的大体逻辑代码如下: private static void findMovieId() throws Exception { File resultFile = new File( ...
-
java.io.IOException: Messenger was closed
程序运行一段时间后抛出异常java.io.IOException: Messenger was closed,不知道是啥原因? ———————————————————————————————————— ...
-
Caused by: java.io.IOException: Filesystem closed的处理
org.apache.hadoop.hive.ql.metadata.HiveException: Unable to rename output from: hdfs://nameservice/u ...
-
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
近期项目中连接蓝牙之后接收蓝牙设备发出的指令功能,在连接设备之后,创建RfcommSocket连接时候报java.io.IOException: read failed, socket might c ...
随机推荐
-
使用context来传递数据,一个context是一系列变量
页面设计工作和python代码分离,所以我们引用模板来实现这个功能. 一.模板实例 下面是一个模板的实例: [python]<html><head><title>O ...
-
mysql学习笔记 第九天
order by ,limit 和where子查询的使用 order by: order by 列名1,[列名2],[列名3]...(结果先按列1进行排序,在列1的相同的情况下,再按照列2的排序,以此 ...
-
pip在windows域下使用代理安装package方法
首先说明下,本人在公司使用windows域账户代理上网,用pip在线安装package 返回ProxyError,类似Tunnel connection failed: 407 authenticat ...
-
CentOs Linux 分区建议
硬盘分区方案 在计算机上安装Linux系统,对硬盘进行分区是一个非常重要的步骤,下面介绍几个分区方案. (1)方案1(桌面)/boot:用来存放与Linux系统启动有关的程序,比如启动引导装载程序等, ...
-
几种必知的oracle结构图
一.数据库结构 二.Oracle 内存结构 三.进程结构 1. 用户进程:在数据库用户请求连接到Oracle 服务器时启动 2. 服务器进程:可以连接到Oracle实例,它在用户建立会话时启动 3. ...
-
wxWidgets帮助文档(1)
IMPLEMENT_APP IMPLEMENT_APP(className) 这是在应用程序中使用类的实现文件的应用程序类wxWidgets动态建筑.你用这个代替Old form: MyApp myA ...
-
《Effective C++》模板与泛型编程:条款32-条款40
条款41:了解隐式接口和编译期多态 class支持显示接口和运行期多态 class的显示接口由函数的名签式构成(函数名称.参数类型.返回类型) class的多态通过virtual函数发生在运行期 te ...
-
UWP开发细节记录:加载图像文件到D2D位图和D3D纹理
在UWP中加载文件一般先创建 StorageFile 对象,然后调用StorageFile.OpenReadAsync 方法得到一个IRandomAccessStream 接口用来读取数据: Stor ...
-
MYSQL数据库链接层- SUMMER-SQL
2015年3月31日 18:27:34 最后编辑: 2016年4月17日 00:22:00 星期日 最后编辑: 2018-4-25 16:58:44 星期三 最新代码: https://gitee.c ...
-
INSTALL_FAILED_CONFLICTING_PROVIDER
主要是由于调试的环境中已有一个同名的Provider存在. 解决方法是修改AndroidManifest.xml中的 <provider android:name=".apps.App ...