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. 参考资料
相关文章
- ABP源码分析四十:ZERO的Application和Tenant
- ArrayList源码分析(jdk1.8)
- 我对java String的理解 及 源码浅析
- Spring+EhCache缓存实例(详细讲解+源码下载)
- MFC Windows 程序设计[285]之文件头过滤控件(附源码)
- Atitit 项目源码管理 attilax著 1. 源码结构sdk目录结构1 1.1. 源码分类,配置文件,主程序文件,sql文件1 2. 源码管理,提交,更新,与同步1 2.1. 源码同步
- ZZNUOJ_用C语言编写程序实现1211:日期排序(附完整源码)
- Android8.0 Settings源码 Preference详解
- WebRTC源码分析 nack详解
- Android之ViewStub源码简析
- android 权限管理详解,EasyPermissions源码浅析
- autoconf-archive源码安装
- linux内核radeon gpu源码解析6 —— radeon_driver_load_kms函数详解1
- 风格迁移0-10:stylegan-源码无死角解读(6)-loss损失函数详解
- Hadoop源码详解之Job 类
- HttpClient` 源码详解之`UrlEncodedFormEntity
- HttpClient 源码详解之HttpRequestBase
- Jdk源码详解之 Iterable 接口
- WebRTC Android源码编译详解
- COVID-Net工程源码详解(三) - inference.py解析
- Harbor 源码浅析
- 01 【Verilog实战】同步FIFO的设计(附源码RTL/TB)