使用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 ...
随机推荐
- 118. Pascal's Triangle (Array)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 关于ueditor使用说明
1.后台配置文件config.json中的各种prefix,相对的都是网站(发布后的)的根目录, 2.需要在iis下将net文件夹转换为应用程序 3.<div> <h1> ...
- 今天无意中发现的WWW.threadPriority
WWW.threadPriority Description Priority of AssetBundle decompression thread. You can control dec ...
- jQuery 复制节点的元素实现添加到购物车功能
描述: 用户点击左边div中的商品,对应商品会自动添加到右面的div中,类似电子商城中的添加到购物车功能. 主要用到了jquery中的复制节点功能,基本原理是首先获取点击的元素,然后将相应信息进行克隆 ...
- jrebel+idea 进行热部署配置
1.安装和激活jrebel这里不在叙说 2.部署项目工程的两种方式 第一:打开项目配置project structure 配置Artificials 第二:tomcat加载项目 然后填写应用名 ...
- Introduction Sockets to Programming in C using TCP/IP
Introduction Computer Network: hosts, routers, communication channels Hosts run applications Routers ...
- gsm
libosmocore Osmocom-BB wireshark 拦截一个短信内容
- [转]WCF Data Services OData
http://martinwilley.com/net/data/wcfds.html WCF Data Services About OData Server code Client For .ne ...
- C++中的关键知识点(汇总)
1. class的virtual 与non-virtual的区别 (1)virtual 函数时动态绑定,而non-virtual是静态绑定,前者是多态效果. (2)多态类的析构函数应该为virtual ...
- Mirth Connect的简单使用
第一步: 切换到Channels界面,右键点击New Channel 第二步 : 上面是设置一些通道信息. 其中summary(概要) 界面主要包含 通道名称,数据类型,依赖,通道初始状态,附件(是否 ...