HttpEntity的类型及其使用(各种继承的使用)

package com.lyj.demo.customTests;import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Map;import static java.nio.charset.StandardCharsets.UTF_8;/*** @author 凌兮* @date 2020/10/8 13:45* HttpEntity相关类测试*/
@RunWith(SpringJUnit4ClassRunner.class)
public class HttpEntityTest {/*** basicHttpEntity测试* 代表底层流的基本实体。通常是在http报文中获取的实体。* 他只有一个空参的构造方法。刚创建时没有内容,长度为负值。* 需要通过两个方法,把值赋进去*/@Testpublic void basicHttpEntityTest() throws IOException {InputStream inputStream = null;// BasicHttpEntity这类就是一个输入流的内容包装类,包装内容的相关的编码格式,长度等BasicHttpEntity basicHttpEntity = new BasicHttpEntity();// 设置内容basicHttpEntity.setContent(inputStream);// 设置长度basicHttpEntity.setContentLength(inputStream.available());// 暂时不明白他是干啥的basicHttpEntity.setChunked(false);}/*** byteArrayEntity测试* 是自我包含的,可重复获得使用的,* 从指定的字节数组中取出内容的实体。* 字节数组是这个实体的构造方法的参数。*/@Testpublic void byteArrayEntityTest() {ByteArrayEntity byteArrayEntity = new ByteArrayEntity("你好呀".getBytes());// 返回byteArrayInputStream对象ByteArrayInputStream content = (ByteArrayInputStream) byteArrayEntity.getContent();/*public InputStream getContent() {return new ByteArrayInputStream(this.b, this.off, this.len);}*/}/*** StringEntity测试* 是自我包含的可重复的实体。通过String创建的实体。* 有两个构造方法,一个是自Sring为参数的构造方法,* 一个是以String和字符编码为参数的构造方法*/@Testpublic void stringEntityTest() throws UnsupportedEncodingException {StringBuilder stringBuilder = new StringBuilder();// 获取系统信息集合,这个集合是不可以修改的Map<String, String> getenv = System.getenv();for (Map.Entry<String,String> entry : getenv.entrySet()) {stringBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");}// 拼接后的系统信息集合String content = stringBuilder.toString();System.out.println(content);// 创建只带字符串参数的entityStringEntity entity = new StringEntity(content);// 创建带字符串参数的和指定的字符编码格式的entityStringEntity entity1 = new StringEntity(content, UTF_8);}/*** InputStream测试* 是流式不可以重复的实体。* 构造方法是InputStream 和内容长度,内容长度是输入流的长度。*/@Testpublic void inputStreamEntityTest() throws IOException {InputStream inputStream = null;// InputStreamEntity严格是对内容和长度相匹配的。用法和BasicHttpEntity类似InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream, inputStream.available());}/*** fileEntity测试* 自我包含式,可以重复的实体。参数传入文件和文件类型。*/@Testpublic void fileEntityTest() {FileEntity fileEntity = new FileEntity(new File(""), ContentType.APPLICATION_FORM_URLENCODED);FileEntity fileEntity1 = new FileEntity(new File(""), ContentType.APPLICATION_JSON);// 已经过时了
//        FileEntity fileEntity1 = new FileEntity(new File(""), ContentType.APPLICATION_JSON.getMimeType());System.out.println(ContentType.APPLICATION_JSON.getMimeType());}/*** EntityTemplete测试* 从ContentProducer接口接受内容的实体。* 在ContentProducer的实现类中写入想要写入的内容*/@Testpublic void entityTempleteTest() throws IOException {ContentProducer contentProducer = new ContentProducer() {@Overridepublic void writeTo(OutputStream outputStream) throws IOException {outputStream.write("你好呀".getBytes());}};EntityTemplate entityTemplate = new EntityTemplate(contentProducer);entityTemplate.writeTo(System.out);
//        System.out.println(entityTemplate.getContent());}/*** HttpEntityWrapper测试* 这个是创建被包装实体的基类,有被包装实体的引用。* 相当于实体的代理类,被包装实体是他的一个属性。*/@Testpublic void httpEntityWrapperTest() throws IOException {HttpEntityWrapper httpEntityWrapper = new HttpEntityWrapper(new HttpEntity() {@Overridepublic boolean isRepeatable() {return false;}@Overridepublic boolean isChunked() {return false;}@Overridepublic long getContentLength() {return 0;}@Overridepublic Header getContentType() {return null;}@Overridepublic Header getContentEncoding() {return null;}@Overridepublic InputStream getContent() throws IOException, UnsupportedOperationException {return null;}@Overridepublic void writeTo(OutputStream outputStream) throws IOException {outputStream.write("你好呀".getBytes());}@Overridepublic boolean isStreaming() {return false;}@Overridepublic void consumeContent() throws IOException {}});httpEntityWrapper.writeTo(System.out);System.out.println(httpEntityWrapper.getContent());}/*** BufferedHttpEntity测试* 是HttpEntityWarpper的子类,可以把不可以重复的实体,* 实现成可以重复的实体。它从提供的实体中读取内容,缓存到内容中。*/@Testpublic void bufferedHttpEntityTest() throws IOException {BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(new HttpEntity() {@Overridepublic boolean isRepeatable() {return false;}@Overridepublic boolean isChunked() {return false;}@Overridepublic long getContentLength() {return 0;}@Overridepublic Header getContentType() {return null;}@Overridepublic Header getContentEncoding() {return null;}@Overridepublic InputStream getContent() throws IOException, UnsupportedOperationException {return null;}@Overridepublic void writeTo(OutputStream outputStream) throws IOException {outputStream.write("你好呀".getBytes());}@Overridepublic boolean isStreaming() {return false;}@Overridepublic void consumeContent() throws IOException {}});bufferedHttpEntity.writeTo(System.out);System.out.println(bufferedHttpEntity.getContent());// 入参校验函数
//        Args.notNull();}/*** HttpEntity测试1* HttpEntity实体即可以使流也可以使字符串形式。具体有什么用法看他的方法解释*/@Testpublic void httpEntityTest1() throws IOException {//        new StringEntity();try {HttpEntity entity = new StringEntity("这是一个字符串", StandardCharsets.UTF_8);// 内容类型System.out.println(entity.getContentType());// 内容的编码格式System.out.println(entity.getContentEncoding());// 内容的长度System.out.println(entity.getContentLength());// 内容的长度System.out.println(entity.getContent().available());// 获取流System.out.println(entity.getContent());// 把内容转换成字符串System.out.println(EntityUtils.toString(entity));// 把内容转为字节数组并获取长度System.out.println(EntityUtils.toByteArray(entity).length);System.out.println("----------------------------------");// 获取entity内容类型Header header = entity.getContentType();System.out.println(header);// 内容类型元素HeaderElement[] elements = header.getElements();System.out.println(elements);// 通过entity获取内容类型ContentType contentType = ContentType.get(entity);System.out.println(contentType);} catch (IOException | UnsupportedOperationException | UnsupportedCharsetException | ParseException e) {throw new RuntimeException(e);}}/*** HttpEntity测试2* 对于实体的资源使用完之后要适当的回收资源,特别是对于流实体。*/@Testpublic void httpEntityTest2() throws IOException {HttpResponse response = null;HttpEntity entity = response.getEntity();if (entity != null) {InputStream inputStream = entity.getContent();try {// 做一些流式操作} catch (Exception e) {// 异常捕捉e.printStackTrace();} finally {// 由于InputSteam实现了Closeable接口,可以实现自动关闭流,所以这里可以不用关闭流,if (inputStream != null) {inputStream.close();}// 采用这种方法也可以实现关闭流EntityUtils.consume(entity);}}}
}

参考链接

HttpEntity的类型及其使用(各种继承的使用)相关推荐

  1. .NET面试题解析(04)-类型、方法与继承

    转自:http://www.cnblogs.com/anding/p/5248973.html 常见面试题目: 1. 所有类型都继承System.Object吗? 2. 解释virtual.seale ...

  2. JAVA 【引用类型】和【对象类型】在【继承】中的异同

    介绍 JAVA [引用类型]和[对象类型]在[继承]中的异同.这个问题自己整理过N次.也被人当菜鸟问过N次.所以,在此简单整理一下.以供大家分享. 在继承关系中.一般成员变量是依据引用类型 在继承关系 ...

  3. 类型“XXX”违反了继承安全性规则。派生类型必须与基类型的安全可访问性匹配或者比基类型的安全可访问性低。...

    类型"XXX"违反了继承安全性规则.派生类型必须与基类型的安全可访问性匹配或者比基类型的安全可访问性低. 原文 http://help.jumbotcms.net/detail_2 ...

  4. HttpEntity的类型及其使用

    一 HttpEntity的类型 1  BasicHttpEntity 代表底层流的基本实体.通常是在http报文中获取的实体.他只有一个空参的构造方法.刚创建时没有内容,长度为负值.需要通过两个方法, ...

  5. C#拾遗系列(9):继承、接口、扩展方法、分部类、类操作、Ref and Out、可空类型...

    本文内容: 继承 Equal示例 结构和类 属性 Ref and Out 类操作 扩展方法 接口 可空类型 分部类 1. 继承 using System; using System.Collectio ...

  6. ADO.NET Entity Framework如何:通过每种类型一个表继承以定义模型(实体框架)

    本主题介绍如何手动创建具有每种类型一个表继承层次结构的概念模型.每种类型一个表继承使用数据库中单独的表为继承层次结构中的每种类型维护非继承属性和键属性的数据. 说明: 建议使用 ADO.NET 实体数 ...

  7. python 参数类型的多态_【Python】面向对象:类与对象\封装\继承\多态

    六.Python面向对象--类与对象\封装\继承\多态 1.什么是面向对象编程 1.1 程序设计的范式:程序可控,易于理解 1.2 抽象并建立对象模型 1.3 程序是不同对象相互调用的逻辑.每个对象在 ...

  8. python中的序列类型数据结构元素的切片操作_PythonI/O进阶学习笔记_4.自定义序列类(序列基类继承关系/可切片对象/推导式)...

    前言: 本文代码基于python3 Content: 1.python中的序列类分类 2. python序列中abc基类继承关系 3. 由list的extend等方法来看序列类的一些特定方法 4. l ...

  9. Go 学习笔记(33)— Go 自定义类型 type(自定义结构体、结构体初始化、结构体内嵌、自定义接口)

    1. 自定义类型格式 用户自定义类型使用关键字 type ,其语法格式是: type newType oldType oldType 可以是自定义类型.预声明类型.未命名类型中的任意一种. newTy ...

  10. 自定义的类型转换器中怎样自定义错误消息?(待解答)

    1.HTTP没有"类型"的概念,每一项表单输入只可能是一个字符串或一个字符串数组.从HTML表单到服务器端,必须把String转换为特定的数据类型. 2.字符串和基本数据类型之间的 ...

最新文章

  1. 全面解读数据中台,让企业实现数字化转型
  2. jqgrid的函数与操作
  3. ueditor html中使用方法,vue集成百度UEditor富文本编辑器使用教程
  4. PHP使用文件流下载文件方法(附:解决下载文件内容乱码问题)
  5. 面试突击 | 彻底搞定 JVM 这几道高频面试题
  6. 数据结构与算法之四希尔排序法
  7. java之tomcat搭建文件服务器
  8. Apache Hadoop 项目介绍
  9. 【20保研】2019年中科院沈阳计算所大学生暑期夏令营通知
  10. MySQL 数据库扩容方案
  11. jdk7下载、安装与测试
  12. oracle11g安装卡在94,winserver2008R2 安装64位 oracle 11G R2 卡在2%,求解
  13. office2020与2016版的不同_如何解决Office2020与office2020兼容问题
  14. 基于Java的多元化智能选课系统 毕业设计-附源码040909
  15. 关于BD文件的一些操作
  16. Windows下安装anaconda、创建虚拟环境、常见的conda命令
  17. Unity编辑器录屏神器:Unity Recorder
  18. 阿里P1到P10,你的能力能拿多少年薪?
  19. Proteus和Keil C51联调仿真完整解析(附程序)
  20. linux升级内核ivh,Linux内核升级

热门文章

  1. 饥荒联机云服务器_饥荒steam联机版专用服务器搭建
  2. 饥荒服务器文档,建立饥荒服务器
  3. 面板模型进行熵值法分析
  4. 只愿得一人心 白首不分离
  5. ssh2连接linux超时,解决SSH会话连接超时问题
  6. 程序员的自我进化:技术的广度与深度怎么权衡
  7. 淘宝校园笔试题鸡蛋与篮子
  8. OpenStack实践(十一):Instance Live Migrate and Evacuate
  9. 极坐标可以用计算机吗,极坐标
  10. ShareX加七牛云免费搭建快速博客图床