HttpClient 源码详解之HttpEntity

article/2025/9/30 23:35:56

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

http://chatgpt.dhexx.cn/article/NfkxXgy1.shtml

相关文章

System.Net.Http.HttpClient

本文主要是介绍如何用HttpClient请求带参数的服务,请求服务为某翻译API 直接上源码 1.添加using System.Net;的引用 using System.Net; 2.使用HttpClient发送请求 public static async void Fanyin_HttpClient(string fromString) {Console.WriteLine($"F…

ResponseEntity类和HttpEntity及跨平台路径问题

1. 简介 使用spring时,达到同一目的通常有很多方法,对处理http响应也是一样。本文我们学习如何通过ResponseEntity设置http相应内容、状态以及头信息。 ResponseEntity是HttpEntity的扩展,添加一个HttpStatus状态代码。在RestTemplate和Con…

RestTemplate发送HTTP、HTTPS请求

前面我们介绍了如何使用Apache的HttpClient发送HTTP请求,这里我们介绍Spring的Rest客户端(即:RestTemplate) 如何发送HTTP、HTTPS请求。注:HttpClient如何发送HTTPS请求,有机会的话也会再给出示例。 声明:本人一些内容摘录自其他朋友的博客,链接在本文末给出&#…

HttpEntity的用法

关于HttpEntity的用法 HttpEntity表示http的request和resposne实体,它由消息头和消息体组成。 从HttpEntity中可以获取http请求头和回应头,也可以获取http请求体和回应体信息。HttpEntity的使用,与RequestBody 、ResponseBody类似。 HttpEnti…

关于HttpEntity的用法

关于HttpEntity的用法 HttpEntity表示http的request和resposne实体,它由消息头和消息体组成。从HttpEntity中可以获取http请求头和回应头,也可以获取http请求体和回应体信息。 HttpEntity的使用,与RequestBody 、ResponseBody类似。HttpEntit…

查询MySQL数据库中表结构的几种方法

mySQL: 方法一、二、三:是在本数据库中查询。 /** 方法一 **/ show columns from activity_station; /** 方法二 **/ desc activity_station; describe activity_station;/** 方法三 **/ show create table activity_station;方法四是在统计数据库中查询。 informa…

mysql 表结构查询的SQL语句

1、查看表结构 desc student;2、查看表的DDL语句 show create table student;3、查看列的结构信息 select column_name,data_type,column_comment,column_key,extra,character_maximum_length,is_nullable,column_default from information_schema.columns where table_sch…

mysql查看表结构 desc 表名;

查看表结构 语法:desc 表名;

MySQL之表结构

1.添加、删除字段 查看表结构 desc blog;show columns from blog;新增字段 alter table 表名 add 字段名 字段数据类型;alter table blog add age tinyint;删除字段 alter table 表名 drop 字段名;alter table blog drop age;重新添加age alter table blog add age tinyin…

mysql查看表结构和编码

show full columns from XXXXX

查看修改MySQL表结构命令

查看修改MySQL表结构命令 简述 小编经常会遇到一些数据库编码不对得问题,好TM头疼,这里做一个记录,供大家参考。 修改数据库字符集: ALTER DATABASE db_name DEFAULT CHARACTER SET character_name [COLLATE …]; 把表默认的字…

Mysql数据库常用表结构操作语句

目录 一、准备工作二、常用表结构操作语句1.在数据库表中新增 字段、约束1.1 新增字段1.2 新增约束 2.在数据库表中修改 表、字段2.1 修改表的名字2.2 修改字段名、数据类型、默认值、注释、自增属性 3.在数据库表中删除表、字段3.1 删除表3.2删除字段、字段约束 3.在数据库表中…

mysql 查询所有表结构_mysql数据库查看表结构

Mysql技术知识点 Mysql技术知识点,首先来讲一下Mysql语句精进安装数据库,然后导入tigerfive.sql。 sql见文档结尾部分MySQL示例数据库模式由以下表组成: customers: 存储客户的数据。 products: 存储汽车的数据。 productLines: 存储产品类别数据。 orde... 文章 好程序员 2…

linux mysql 结构_linux查看mysql数据表结构

展开全部 一、简单描述表结构,字段类型 desc tabl_name; 显示32313133353236313431303231363533e78988e69d8331333365636133表结构,字段类型,主键,是否为空等属性,但不显示外键。 例如:desc table_name 二、…

mysql查看表结构修改记录表_MySQL 表结构查看以及表结构修改

使用MySql数据库时,如果对表字典不够熟悉,我们就需要查看表结构。如果数据库是在远程linux上部署,很多时候我们只能通过mysql的命令来查看表结构。同样,如果我们想要修改表结构,添加、删除、修改列时,也只能…

mysql查看表描述_MySQL 查看表结构简单命令

一、简单描述表结构,字段类型 desc tabl_name; 显示表结构,字段类型,主键,是否为空等属性,但不显示外键。 例如:desc table_name 二、查询表中列的注释信息 select * from information_schema.columns where table_schema = db #表所在数据库 and table_name = tablename…

mysql查看表结构的几种方式

在我第N次忘记如何查看表结构后,在网上查了一下后,看到有好几种查看表结构的方式,总结一下。 以student(sid,sname,birthday,sex)的查看为例。 【方式一】:desc student; 语法:desc 表名;-----…

【Jon】连接Mysql数据库出现1046错误

问题:今天博主在使用Navicat12连接Mysql数据库的时候出现1046-UDAL-No database selected错误。 解决办法:直接换了一个版本的Navicat,就解决了问题。Nice^^

MYSQL 1064 错误

用MySQL执行批量插入数据,格式如下: declare i integer ; -- 定义变量begin i : 0000;loop /* 插入数据 */ insert into 表名 (字段1,字段2,... ) values (字段1值,字段2值,...); /* 参数递增 */ i : i 1;/* 停止条件 */ exit when i > 000111; …

【报错问题解决】MySQL创建表报错 ERROR 1046 (3D000): No database selected

【报错问题解决】MySQL创建表报错 ERROR 1046 : No database selected 问题概述原因解决方法 关于数据库和表的创建请参考文章 【手把手】MySQL创建数据库和表 问题概述 数据库已经创建 创建表的时候报错:ERROR 1046 (3D000): No database selected 原因 创建…