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. 参考资料

  • http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/org/apache/http/HttpEntity.html
  • https://bbs.csdn.net/topics/392259303?list=lz

HttpClient 源码详解之HttpEntity相关推荐

  1. 【Live555】live555源码详解(九):ServerMediaSession、ServerMediaSubsession、live555MediaServer

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的三个类所在的位置: ServerMediaSession.ServerMediaSubsession.Dy ...

  2. 【Live555】live555源码详解系列笔记

    [Live555]liveMedia下载.配置.编译.安装.基本概念 [Live555]live555源码详解(一):BasicUsageEnvironment.UsageEnvironment [L ...

  3. 【Live555】live555源码详解(八):testRTSPClient

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的testRTSPClient实现的三个类所在的位置: ourRTSPClient.StreamClient ...

  4. 【Live555】live555源码详解(七):GenericMediaServer、RTSPServer、RTSPClient

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的三个类所在的位置: GenericMediaServer.RTSPServer.RTSPClient 14 ...

  5. 【Live555】live555源码详解(六):FramedSource、RTPSource、RTPSink

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的三个类所在的位置: FramedSource.RTPSource.RTPSink 11.FramedSou ...

  6. 【Live555】live555源码详解(五):MediaSource、MediaSink、MediaSession、MediaSubsession

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的四个类所在的位置: MediaSource.MediaSink.MediaSession.MediaSub ...

  7. 【Live555】live555源码详解(四):Medium媒体基础类

    [Live555]live555源码详解系列笔记 7.Media Medai所依赖关系图 依赖Medai关系图 Media和UsageEnvironment关联图

  8. 【Live555】live555源码详解(二):BasicHashTable、DelayQueue、HandlerSet

    [Live555]live555源码详解系列笔记 3.BasicHashTable 哈希表 协作图: 3.1 BasicHashTable BasicHashTable 继承自 HashTable 重 ...

  9. 【Live555】live555源码详解(一):BasicUsageEnvironment、UsageEnvironment

    [Live555]live555源码详解系列笔记 类关系图 1.UsageEnvironment 详解 1.1 BasicUsageEnvironment BasicUsageEnvironment ...

最新文章

  1. 【tensorflow】OP_REQUIRES failed at variable_ops.cc:104 Already exists: Resource
  2. C++中 何时用. 何时用-」
  3. kafka 在阿里云部署
  4. 微信公众号怎么快速导出一个月的文章数据
  5. C++ Primer 第五版 第7章类 7.1——类讲解(成员函数、非成员函数、构造函数)
  6. 字符界面运行mysql_详解linux系列之字符界面下MySQL+apache+php的源代码安装
  7. 判断触摸的点在那个 View上
  8. python aes加密对于长字符数据丢失_Python 3中AES加密和解密的字符串字节数
  9. [状压dp][剪枝搜索] 洛谷 P2831 愤怒的小鸟
  10. icem网格划分如何给内部面网格,ICEM CFD处理混合网格划分中低质量的问题
  11. 关于银联支付和提现切换服务器出现的种种问题
  12. 吾爱IC社区知识星球问答精选
  13. python曲线拟合准确度评估_使用Python SciPy量化曲线拟合的质量
  14. 企业如何使用OA系统?OA系统有哪些功能和应用的场景?
  15. 浅谈软件可视化分析内存消耗综述中的识别方法
  16. 关于《小萝莉的猴神大叔》些许体会
  17. David Silver强化算法学习地址和PPT
  18. 【NVMe2.0b 13】NVMe 标准数据结构
  19. spec2006 测试
  20. 单片机播放WAV格式音频的理解

热门文章

  1. 安卓应用平台想要反超iOS?在中国好好表现吧
  2. 人生男女的30个经典幽默
  3. 【GDKOI】2021普及Day2
  4. 【算法设计与分析】算法的时间复杂度(介绍O渐近上界,Ω渐近下界,θ准确的界)
  5. Windows10系统下swin-transformer目标检测环境搭建
  6. 努比亚z9max升级安卓7.1.2版本低配也能吃鸡
  7. 亲爱的自己请早些离开把
  8. Resilient Distributed Datasets: A Fault-Tolerant Abstraction for In-Memory Cluster Computing
  9. 100道Java笔试题
  10. 全连接神经网络反向传播详解