Attempted read from closed stream.

最近项目中用到 HttpClient ,代码如下,我用的是 get的方式:

  public static String  get() throws IOException, InterruptedException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpEntity entity = null;
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet("http://127.0.0.1:2333/personal-api/api/personal/signn/personss?phone=11111111&name=hxy")
            httpget.setHeader("User-Agent","Mozilla/5.0");
            httpget.setHeader("Content-Type","application/json");
            System.out.println("executing request " + httpget.getURI());
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
                 entity = response.getEntity();
                System.out.println("--------------------------------------");
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
                    System.out.println("Response content length: " + entity.getContentLength()); 
                    // 打印响应内容
                    System.out.println("Response content: " + EntityUtils.toString(entity)); //这里要多留意一下,下面会说到
                }
                System.out.println("------------------------------------");
                return EntityUtils.toString(entity); //这是返回值
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
               httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    } 
 //main 方法:
    public static void main(String[] args) throws InterruptedException, IOException {
        String s1 = get();
        System.out.println("打印的是: "+s1);//打印返回值内容
    }

运行 main方法,会出现以下异常:

java.io.IOException: Attempted read from closed stream.
 at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:165)
 at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
 at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
 at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
 at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
 at java.io.InputStreamReader.read(InputStreamReader.java:184)
 at java.io.Reader.read(Reader.java:140)
 at org.apache.http.util.EntityUtils.toString(EntityUtils.java:225)
 at org.apache.http.util.EntityUtils.toString(EntityUtils.java:306)
 at cc.huluwa.windowmain.config.TestHttpClients.get(TestHttpClients.java:105)
 at cc.huluwa.windowmain.config.TestHttpClients.main(TestHttpClients.java:73)

出现这样的错误因为:
EntityUtils.toString(entity);这句导致了后面的写入文件错误.entity所得到的流是不可重复读取的也就是说所得的到实体只能一次消耗完,不能多次读取,所以在执行EntityUtils.toString(entity)后,流就关闭了,就导致后面的读和写显示错误.
解决方法:
System.out.println(“Response content: ” + EntityUtils.toString(entity));这句话注释掉,不要打印在控制台

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值