使用HttpClient出现java.io.IOException: Attempted read from closed stream
问题描述:
使用httpClient时候,出现java.io.IOException: Attempted read from closed stream.
原始代码:
public static String postJosn(String url, String jsonString) throws Exception { SSLContext sslContext = SSLContexts.custom().useTLS().build();
SSLConnectionSocketFactory f = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" }, null,
null);
CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(f).build();
// 设置请求超时时间 15秒
//client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
HttpPost myPost = new HttpPost(url);
myPost.setHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
myPost.setHeader("charset", "utf-8"); StringEntity s = new StringEntity(jsonString, "utf-8");
s.setContentEncoding("UTF-8");
s.setContentType("application/json;charset=utf-8");
myPost.addHeader("Content-Type", "application/json;charset=utf-8");
myPost.setEntity(s); HttpResponse res = client.execute(myPost);
HttpEntity entity = res.getEntity();
//myPost.releaseConnection();
log.info(EntityUtils.toString(entity, "utf-8"));;
return EntityUtils.toString(entity, "utf-8");
}
原因分析:
EntityUtils.toString(HttpEntity entity, String defaultCharset)方法中操作的是流数据,流数据是一次性数据所以同一个HttpEntity不能使用多次该方法.
源码:
使用HttpClient出现java.io.IOException: Attempted read from closed stream的更多相关文章
- java.io.IOException: Attempted read from closed stream
前言: 代码如下,执行的时候提示"java.io.IOException: Attempted read from closed stream." public static JS ...
- java.io.IOException: Attempted read from closed stream解决
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...
- 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)方法被使用了多次.所以每个方法内只能使用一次.
- mockito 异常Reason: java.io.IOException: invalid constant type: 18
原因: mockito内部使用的javassit的版本不一致导致的,修改为一直版本即可. 异常内容: /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jd ...
- java.io.IOException: mark/reset not supported
java.io.IOException: mark/reset not supported at java.io.InputStream.reset(InputStream.java:348) at ...
- java.io.IOException: invalid header field
通过本文, 我们明白了什么是 jar的清单文件 MANIFEST.MF, 简单示例: E:\ws\Test\WEB-INF\classes>jar cvfm testCL.jar ListTes ...
- java.io.IOException: Cannot run program "jad"
今天调试google tag manager, 需要看看google analytics source code,无奈没有源码,装个一个插件ejad 还是不行: java.io.IOException ...
- org.apache.hadoop.ipc.RemoteException(java.io.IOException)
昨晚突然之间mr跑步起来了 jps查看 进程都在的,但是在reduce任务跑了85%的时候会抛异常 异常情况如下: 2016-09-21 21:32:28,538 INFO [org.apache.h ...
随机推荐
- mybatis使用foreach进行批量插入和删除操作
一.批量插入 1.mapper层 int insertBatchRoleUser(@Param("lists") List<RoleUser> lists);//@Pa ...
- Common issue on financial information exchange (FIX) Connectivity[z]
FIX Protocol Session Connectivity Hi guys, in this post I would like share my experience with financ ...
- 配置Linux防火墙
配置防火墙(服务器安全优化)(转) 安全规划:开启 80 22 端口并 打开回路(回环地址 127.0.0.1) # iptables –P INPUT ACCEPT # iptables –P O ...
- ManualResetEvent,AutoResetEvent说明
相信不少人对ManualResetEvent,AutoResetEvent的状态比较晕,下面是本人认为最精简的理解 1.只有2种状态,终止态 And 非终止态 终止状态,既然是状态那么一定对应事物,这 ...
- ffmpeg只编译h264
./configure --arch=arm --cross-prefix=arm-none-linux-gnueabi- --extra-ldflags=-static --target-os=li ...
- linux平台 spark standalone集群 使用 start-all,stop-all 管理集群的启动和退出
一.配置/etc/profile: 文件尾部增加以下内容: export SPARK_HOME=/home/spark/spark-2.2.0-bin-hadoop2.7 export PATH=$P ...
- linux每天一小步---cp命令详解
1 命令功能 cp命令用于复制文件或者目录,cp是copy的缩写. 2 命令语法 cp [参数] 源文件或者目录 目的文件或者目录 3 命令参数 -a 等同于-dRp,保存所有 -d ...
- 设置手机iphone5s邮件
由于更新系统后,手机自带的邮件服务器老是报错,后来查一下,需要设置qq邮箱独立密码,http://jingyan.baidu.com/article/c146541354cefb0bfdfc4c5d. ...
- 关于:Warning: skipping non-radio button in group的处理方法整理
下面讲的是一个意思: The problem is that the next control in the tab order following the last radio button of ...
- POJ1087 A Plug for UNIX 2017-02-12 13:38 40人阅读 评论(0) 收藏
A Plug for UNIX Description You are in charge of setting up the press room for the inaugural meeting ...