1. 程式人生 > >HttpClient 原始碼詳解之HttpEntity

HttpClient 原始碼詳解之HttpEntity

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. 參考資料