HttpClient 源码详解之HttpEntity

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/84489320

HttpClient 源码详解 之HttpEntity

1. 类释义

An entity that can be sent or received with an HTTP message. Entities can be found in some requests and in responses, where they are optional.
There are three distinct types of entities in HttpCore, depending on where their content originates:
streamed: The content is received from a stream, or generated on the fly. In particular, this category includes entities being received from a connection. Streamed entities are generally not repeatable.
self-contained: The content is in memory or obtained by means that are independent from a connection or other entity. Self-contained entities are generally repeatable.
wrapping: The content is obtained from another entity.
This distinction is important for connection management with incoming entities. For entities that are created by an application and only sent using the HTTP components framework, the difference between streamed and self-contained is of little importance. In that case, it is suggested to consider non-repeatable entities as streamed, and those that are repeatable (without a huge effort) as self-contained.

可以用Http消息发送或接受的实体。实体可以在一些请求或者响应中被发现,它们是可选的。

2. 方法简介

2.1 isRepeatable()

这个方法可以用于显示该 Entity是否可重复消费

  • 方法释义
    /**
     * Tells if the entity is capable of producing its data more than once.
     * A repeatable entity's getContent() and writeTo(OutputStream) methods
     * can be called more than once whereas a non-repeatable entity's can not.
     * @return true if the entity is repeatable, false otherwise.
     */
    boolean isRepeatable();
  • 类实例
public static void test4() {
        CloseableHttpClient httpClient = HttpClients.createDefault();//创建httpClient实例
        HttpGet httpGet = new HttpGet("http://www.csdn.net"); //创建httpGet实例
        CloseableHttpResponse response = null;//指向http get请求
        try {
            response = httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();//获取返回实体
        System.out.println(entity.isRepeatable());//是否可重复消费

        try {
            response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

main()方法中调用test4()方法,得到的结果如下:
在这里插入图片描述
如果是不可重复消费的实体,那么如果我们超过一次消费的时候就会报错,对应修改程序如下:

public static void test4() {
        CloseableHttpClient httpClient = HttpClients.createDefault();//创建httpClient实例
        HttpGet httpGet = new HttpGet("http://www.csdn.net"); //创建httpGet实例
        CloseableHttpResponse response = null;//指向http get请求
        try {
            response = httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity();//获取返回实体
        System.out.println(entity.isRepeatable());//

        try {
            System.out.println("1:"+EntityUtils.toString(entity).substring(0,10));
            System.out.println("2:"+EntityUtils.toString(entity).substring(0,10));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

执行结果如下:
在这里插入图片描述

3. 参考资料

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/84489320