zoukankan      html  css  js  c++  java
  • 多次读取HttpEntity内容

    有时,需要重复读取HttpEntity,直接使用是行不通的,这时需要使用BufferedHttpEntity类将其进行包装一下。

    public static void main(String[] args) throws IOException {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpGet httpget = new HttpGet("http://localhost:9999/index");
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out.println("==============重复使用response entity================================");
                    entity = new BufferedHttpEntity(entity);
                    System.out.println(EntityUtils.toString(entity));
                    System.out.println(EntityUtils.toString(entity));
    
                }
            } finally {
                response.close();
            }
        }

    其原理也很简单,直接看下源码

    (1) 使用了一个byte[] 将entity的内容缓存起来

      public BufferedHttpEntity(final HttpEntity entity) throws IOException {
            super(entity);
            if (!entity.isRepeatable() || entity.getContentLength() < 0) {
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                entity.writeTo(out);
                out.flush();
                this.buffer = out.toByteArray();
            } else {
                this.buffer = null;
            }
        }

    将entity 写到 ByteArrayOutputStream 对象,然后转成byteArray存起来, this.buffer 就是一个byte[]。

    (2)读取内容时直接读取缓存byte[]中的内容 

    而getContent()源码如下:

      public InputStream getContent() throws IOException {
            return this.buffer != null ? new ByteArrayInputStream(this.buffer) : super.getContent();
        }

    由第一步知,buffer不为null,  所以每次读取内容时,都会创建一个ByteArrayInputStream对象!从而间接的实现了重复使用enity的目的。

  • 相关阅读:
    fdisk 分区
    fdisk 添加逻辑分区
    centos7 bond0 双网卡配置
    查看centos7启动项
    本地yum源安装docker
    cobbler Ubuntu16.04 安装
    docker-ce-17.03.2 离线安装RPM包
    day14 生成器的进阶
    day13迭代器与生成器
    day12闭包,装饰器
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/11706291.html
Copyright © 2011-2022 走看看