问题描述:

使用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的更多相关文章

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

    前言: 代码如下,执行的时候提示"java.io.IOException: Attempted read from closed stream." public static JS ...

  2. java.io.IOException: Attempted read from closed stream解决

    在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...

  3. java.io.IOException: Attempted read from closed stream. 异常,解决

    在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...

  4. 在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决

    原因是EntityUtils.toString(HttpEntity)方法被使用了多次.所以每个方法内只能使用一次.

  5. mockito 异常Reason: java.io.IOException: invalid constant type: 18

    原因: mockito内部使用的javassit的版本不一致导致的,修改为一直版本即可. 异常内容: /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jd ...

  6. java.io.IOException: mark/reset not supported

    java.io.IOException: mark/reset not supported at java.io.InputStream.reset(InputStream.java:348) at ...

  7. java.io.IOException: invalid header field

    通过本文, 我们明白了什么是 jar的清单文件 MANIFEST.MF, 简单示例: E:\ws\Test\WEB-INF\classes>jar cvfm testCL.jar ListTes ...

  8. java.io.IOException: Cannot run program "jad"

    今天调试google tag manager, 需要看看google analytics source code,无奈没有源码,装个一个插件ejad 还是不行: java.io.IOException ...

  9. org.apache.hadoop.ipc.RemoteException(java.io.IOException)

    昨晚突然之间mr跑步起来了 jps查看 进程都在的,但是在reduce任务跑了85%的时候会抛异常 异常情况如下: 2016-09-21 21:32:28,538 INFO [org.apache.h ...

随机推荐

  1. jQuery的表单选择器

    1.常规选择器选择表单标签 $(function () { // var a = $("input").eq(0).val() // alert(a) // // var b = ...

  2. ireport related

    http://blog.csdn.net/q326527970/article/details/7049047

  3. VMware CentOS LVM磁盘扩容

    一. 在虚拟机上增加磁盘空间 如下图. 增加完后会有提示 "磁盘已成功扩展.您必须从客户机操作系统内部对磁盘重新进行分区和扩展文件系统.是继续完成以下步骤才算成功. 二.调整虚拟机磁盘LVM ...

  4. 启动memcached

    /usr/local/bin/memcached -d -c -m -u root

  5. [Jenkins]怎样在Jenkins上面启动服务器上的批处理脚本

    New Item 在Build --> Execute Windows batch command --> 里面填写: schtasks /run /tn Start_Hub_szotqa ...

  6. 设置div中的div居中显示

    设置div中的div居中显示 方法一. <div class='big'> <div class='small'>box1</div> </div> s ...

  7. Android Studio修改默认Activity继承AppCompatActivity

    在Android Studio中新建Activity默认继承AppCompatActivity,感觉这点十分不爽,找了很久,终于发现在android Studio安装目录下有个模板文件,修改其中的参数 ...

  8. struts2 入门程序

    1.struts 2.5.2 基本jar包 2.web.xml <!-- Filters --> <!-- START SNIPPET: filter --> <filt ...

  9. I-Keyboard

    SPOJ Problem Set (classical) 14. I-Keyboard Problem code: IKEYB Most of you have probably tried to t ...

  10. B-Spline 样条学习笔记

    (1) 对于clamped样条曲线,节点区间的数目等于曲线段的数目. eg: B-样条曲线有11个控制点(即,n = 10), 3次P样条 (即, p=3)  ,由 m=n+p+1 则有15 个节点  ...