BasicHttpEntity
代表底层流的基本实体。通常是在http报文中获取的实体。他只有一个空参的构造方法。
刚创建时没有内容,长度为负值。需要通过两个方法,把值赋进去。
- /**
- * BasicHttpEntity
- * @throws IOException
- */
- public static void testBasicHttpEntity() throws IOException{
- InputStream is = null;
- //BasicHttpEntity这类就是一个输入流的内容包装类,包装内容的相关的编码格式,长度等
- BasicHttpEntity entity = new BasicHttpEntity();
- //设置内容
- entity.setContent(is);
- //设置长度
- entity.setContentLength(is.available());
- //没搞懂chunked这个属性啥意思
- entity.setChunked(false);
- }
ByteArrayEntity
是自我包含的,可重复获得使用的,从指定的字节数组中取出内容的实体。
字节数组是这个实体的构造方法的参数
- /**
- * ByteArrayEntity
- * @throws IOException
- */
- public static void testByteArrayEntity() throws IOException{
- ByteArrayEntity entity = new ByteArrayEntity("内容".getBytes());
- ByteArrayInputStream is = (ByteArrayInputStream) entity.getContent();
- //上面这行代码返回的其实是一个ByteArrayInputStream对象
- /*public InputStream getContent() {
- return new ByteArrayInputStream(this.b, this.off, this.len);
- }*/
- }
StringEntity
是自我包含的可重复的实体。通过String创建的实体
有两个构造方法,一个是自Sring为参数的构造方法,一个是以String和字符编码为参数的构造方法。
- /**
- * StringEntity
- * @throws IOException
- */
- public static void testStringEntity() throws IOException{
- StringBuilder sb = new StringBuilder();
- //获取系统的信息集合,这个集合是不可以修改的
- Map<String, String> nev = System.getenv();
- for(Entry<String, String> entry : nev.entrySet()){
- sb.append(entry.getKey()).append("=")
- .append(entry.getValue()).append("\n");
- }
- String content = sb.toString();
- System.out.println(content);
- //创建只带字符串参数的
- StringEntity entity = new StringEntity(content);
- //创建带字符创参数和字符编码的
- StringEntity entity2 = new StringEntity(content, "UTF-8");
- }
InputreamEntity
是流式不可以重复的实体。构造方法是InputStream 和内容长度,内容长度是输入流的长度
- /**
- * InputStreamEntity
- * @throws IOException
- */
- public static void testInputStreamEntity() throws IOException{
- InputStream is = null;
- //InputStreamEntity严格是对内容和长度相匹配的。用法和BasicHttpEntity类似
- InputStreamEntity entity = new InputStreamEntity(is, is.available());
- }
FileEntity
自我包含式,可以重复的实体。参数传入文件和文件类型。
- /**
- * FileEntity
- * @throws IOException
- */
- public static void testFileEntity() throws IOException{
- FileEntity entity = new FileEntity(new File(""), ContentType.APPLICATION_FORM_URLENCODED);
- FileEntity entity2 = new FileEntity(new File(""), "application/java-achive");
- }
EntityTemplete
从ContentProducer接口接受内容的实体。
在ContentProducer的实现类中写入想要写入的内容。
- /**
- * EntityTemplate
- * @throws IOException
- */
- public static void testEntityTemplate() throws IOException{
- ContentProducer producer = new ContentProducer() {
- @Override
- public void writeTo(OutputStream outstream) throws IOException {
- outstream.write("这是什么东东》。".getBytes());
- }
- };
- EntityTemplate entity = new EntityTemplate(producer);
- entity.writeTo(System.out);
- }
HttpEntityWrapper
这个是创建被包装实体的基类,有被包装实体的引用。
相当于实体的代理类,被包装实体是他的一个属性。
下面是这个类的源码:
- /*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
- package org.apache.http.entity;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import org.apache.http.Header;
- import org.apache.http.HttpEntity;
- import org.apache.http.annotation.NotThreadSafe;
- /**
- * Base class for wrapping entities.
- * Keeps a {@link #wrappedEntity wrappedEntity} and delegates all
- * calls to it. Implementations of wrapping entities can derive
- * from this class and need to override only those methods that
- * should not be delegated to the wrapped entity.
- *
- * @since 4.0
- */
- @NotThreadSafe
- public class HttpEntityWrapper implements HttpEntity {
- /** The wrapped entity. */
- protected HttpEntity wrappedEntity;
- /**
- * Creates a new entity wrapper.
- *
- * @param wrapped the entity to wrap, not null
- * @throws IllegalArgumentException if wrapped is null
- */
- public HttpEntityWrapper(HttpEntity wrapped) {
- super();
- if (wrapped == null) {
- throw new IllegalArgumentException
- ("wrapped entity must not be null");
- }
- wrappedEntity = wrapped;
- } // constructor
- public boolean isRepeatable() {
- return wrappedEntity.isRepeatable();
- }
- public boolean isChunked() {
- return wrappedEntity.isChunked();
- }
- public long getContentLength() {
- return wrappedEntity.getContentLength();
- }
- public Header getContentType() {
- return wrappedEntity.getContentType();
- }
- public Header getContentEncoding() {
- return wrappedEntity.getContentEncoding();
- }
- public InputStream getContent()
- throws IOException {
- return wrappedEntity.getContent();
- }
- public void writeTo(OutputStream outstream)
- throws IOException {
- wrappedEntity.writeTo(outstream);
- }
- public boolean isStreaming() {
- return wrappedEntity.isStreaming();
- }
- /**
- * @deprecated (4.1) Either use {@link #getContent()} and call {@link java.io.InputStream#close()} on that;
- * otherwise call {@link #writeTo(OutputStream)} which is required to free the resources.
- */
- @Deprecated
- public void consumeContent() throws IOException {
- wrappedEntity.consumeContent();
- }
- }
BufferedHttpEntity
是HttpEntityWarpper的子类,可以把不可以重复的实体,实现成可以重复的实体。
它从提供的实体中读取内容,缓存到内容中。
源码如下:
- /*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
- package org.apache.http.entity;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import org.apache.http.HttpEntity;
- import org.apache.http.annotation.NotThreadSafe;
- import org.apache.http.util.EntityUtils;
- /**
- * A wrapping entity that buffers it content if necessary.
- * The buffered entity is always repeatable.
- * If the wrapped entity is repeatable itself, calls are passed through.
- * If the wrapped entity is not repeatable, the content is read into a
- * buffer once and provided from there as often as required.
- *
- * @since 4.0
- */
- @NotThreadSafe
- public class BufferedHttpEntity extends HttpEntityWrapper {
- private final byte[] buffer;
- /**
- * Creates a new buffered entity wrapper.
- *
- * @param entity the entity to wrap, not null
- * @throws IllegalArgumentException if wrapped is null
- */
- public BufferedHttpEntity(final HttpEntity entity) throws IOException {
- super(entity);
- if (!entity.isRepeatable() || entity.getContentLength() < 0) {
- this.buffer = EntityUtils.toByteArray(entity);
- } else {
- this.buffer = null;
- }
- }
- @Override
- public long getContentLength() {
- if (this.buffer != null) {
- return this.buffer.length;
- } else {
- return wrappedEntity.getContentLength();
- }
- }
- @Override
- public InputStream getContent() throws IOException {
- if (this.buffer != null) {
- return new ByteArrayInputStream(this.buffer);
- } else {
- return wrappedEntity.getContent();
- }
- }
- /**
- * Tells that this entity does not have to be chunked.
- *
- * @return <code>false</code>
- */
- @Override
- public boolean isChunked() {
- return (buffer == null) && wrappedEntity.isChunked();
- }
- /**
- * Tells that this entity is repeatable.
- *
- * @return <code>true</code>
- */
- @Override
- public boolean isRepeatable() {
- return true;
- }
- @Override
- public void writeTo(final OutputStream outstream) throws IOException {
- if (outstream == null) {
- throw new IllegalArgumentException("Output stream may not be null");
- }
- if (this.buffer != null) {
- outstream.write(this.buffer);
- } else {
- wrappedEntity.writeTo(outstream);
- }
- }
- // non-javadoc, see interface HttpEntity
- @Override
- public boolean isStreaming() {
- return (buffer == null) && wrappedEntity.isStreaming();
- }
- } // class BufferedHttpEntity
相关推荐
在Spring MVC框架中,HttpEntity和ResponseEntity是两个非常重要的概念,它们主要用于处理HTTP请求和响应。本项目“springMVC-HttpEntity(ResponseEntity)demo”是一个实战演示,展示了如何在Spring MVC应用中使用...
在提供的信息中,我们关注的是两个特定的模块:"org.apache.http.entity.mime" 和 "org.apache.http.legacy"。 首先,`org.apache.http.entity.mime` 是 Apache HttpClient 库的一个子模块,专门用于处理MIME(多...
HttpEntity entity = builder.build(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entity); ``` 这个例子中,`addTextBody`和`addBinaryBody`分别用于添加文本和二进制数据,`ContentType.create...
它提供了丰富的API来创建、解析和处理MIME类型的HTTP请求和响应。在Android开发中,HttpMime 4.1.2.jar是一个重要的依赖库,它帮助开发者高效地处理HTTP通信中的多媒体数据,如图片、音频、视频等。 1. **HttpMime...
- `HttpEntity`:表示HTTP消息中的实体,可以是请求体或响应体。 - `EntityUtils`:提供了一些实用方法,如读取实体内容、关闭实体等。 在使用Apache HTTP客户端库时,你需要先创建`HttpClient`实例,然后构造`...
import org.apache.http.HttpEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; // 创建MultipartEntityBuilder ...
**WCF WebHttp REST Entity Service** 是一个基于Windows Communication Foundation (WCF) 技术的Web服务实现,它专门设计用于构建RESTful风格的应用程序。REST(Representational State Transfer)是一种架构风格,...
3. **HttpEntity**:表示HTTP消息实体,可以是请求或响应中的数据。它可以是文本、二进制数据或流。 4. **BasicNameValuePair和NameValuePair**:用于构建HTTP请求参数,常见于POST请求的表单数据。 5. **...
此包中的主要类包括`CloseableHttpClient`(负责创建和管理HTTP客户端实例)、`HttpGet`、`HttpPost`(用于构造不同类型的HTTP请求)和`CloseableHttpResponse`(用于接收和处理HTTP响应)。 `httpcomponents-core-...
WCF服务可以通过配置文件定义其行为,包括端点地址、绑定类型和合同类型,这些服务可以被其他应用程序通过网络调用。 **Entity Framework** Entity Framework是一个对象关系映射(ORM)框架,它简化了.NET开发者与...
WCF是.NET框架的一个重要组成部分,它提供了一种统一的服务模型,可以创建各种类型的分布式应用程序。通过WCF,开发者能够构建高度可配置、跨平台的服务,这些服务可以通过多种传输协议(如HTTP、TCP)进行通信。 ...
2. 处理MIME类型的HTTP响应:库中的HttpEntity类及其子类,如BasicHttpEntity和FileEntity,用于处理HTTP响应中的实体内容。这些实体可以是简单的文本,也可以是复杂的MIME多部分数据,如附件下载。 3. 文件上传与...
- **概念**:一种用于实现继承模式的方法,其中每个派生类型都有自己的表。 - **实现**:通过配置`DbSet`的`EntityTypeConfiguration`来指定继承关系。 **2-9 使用条件过滤对象集** - **概念**:根据特定条件过滤...
HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } finally { response.close(); } ``` 总的来说,Apache HttpClient是Java开发中处理HTTP请求的标准工具之一,其丰富的功能和易用性使...
HttpEntity<Map, String>> entity = new HttpEntity<>(requestBody, headers); ResponseEntity<String> response = restTemplate.exchange( "http://example.com/api/resource", HttpMethod.POST, entity, String....
- `HttpEntityMethodProcessor`:处理`HttpEntity`类型的参数,无论是在请求还是响应中。`HttpEntity`代表了一个HTTP消息实体,可以包含头信息和主体。 - `RequestResponseBodyMethodProcessor`:支持`@Request...
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers); // 设置请求头 ResponseEntity<MyResponse> response = restTemplate.postForEntity(url, entity, MyResponse.class); // 发送请求并转换为...
5. **构建并获取`HttpEntity`对象**: ```java HttpEntity entity = builder.build(); ``` 6. **创建`HttpPost`请求**: ```java HttpPost httpPost = new HttpPost("http://your/upload/url"); ...
3. **控制器**:介绍如何创建控制器,处理HTTP请求,并返回视图或JSON等不同类型的响应。 4. **视图**:讲解视图的创建和渲染,以及如何使用razor语法来动态生成HTML。 5. **模型绑定**:说明如何将HTTP请求数据自动...
4. `HttpEntity`:代表HTTP消息体,包含了所有可能的HTTP内容类型。 四、使用示例 以下是一个简单的使用httpmime-4.25.jar发送POST请求并上传文件的Java代码示例: ```java CloseableHttpClient httpClient = ...