码迷,mamicode.com
首页 > Web开发 > 详细

HttpComponents 基础接口/类与HTTP message的对应关系

时间:2016-11-06 02:07:54      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:headers   pat   ptr   define   pac   final   应用   ica   设置   

前提一:

什么是HTTP message -- 遵循HTTP协议发送的消息!其格式是固定的:HTTP message = Header + Body(optional)

HTTP request 的header 由一个请求行和一组header字段组成。

HTTP response 的header 由一个状态行和一组header字段组成。

所有的HTTP message 都必须包含协议版本!某些HTTP message也可以包含一个content body(内容体)。

说明:上面提到的 “Body(optional)” 和 “content body” 是一回事,都是消息的内容体,也被叫做 “Entity”。对于HTTP message来说,”content body” /” entity”不是必须的,而是可选的,具体看情况需要 。

 

前提二:

HttpComponents可以认为主要是两大块,一块HttpCore,一块HttpClient。前者对应HTTP协议,后者则是应用--HTTP客户端编程。

HttpCore 遵循”前提一”的HTTP message定义来定义了HTTP message object,并支持对HTTP message 元素的序列化(格式化)和反序列化(解析)。

 

正文

首先是 HttpMessage 接口,一切的开始。

public interface HttpMessage {

    ProtocolVersion getProtocolVersion();

    boolean containsHeader(String name);

    Header[] getHeaders(String name);

    Header getFirstHeader(String name);

    Header getLastHeader(String name);

    Header[] getAllHeaders();

    void addHeader(Header header);

    void addHeader(String name, String value);

    void setHeader(Header header);

    void setHeader(String name, String value);

    void setHeaders(Header[] headers);

    void removeHeader(Header header);

    void removeHeaders(String name);

    HeaderIterator headerIterator();

    HeaderIterator headerIterator(String name);

    @Deprecated
    HttpParams getParams();

    @Deprecated
    void setParams(HttpParams params);

}

很简单,除了一个获取协议版本的方法,还有params的SETTER/GETTER方法(已过时)之外,都是操作Headers的方法。

从上面无法直接区分request和response,所以要看其子接口 HttpRequest 和  HttpResponse 。

HttpRequest很简单,只多了一个获取请求行的方法。

public interface HttpRequest extends HttpMessage {

    RequestLine getRequestLine();

}

HttpResponse则多了很多方法。涉及到:状态行、状态码、entity、locale。

public interface HttpResponse extends HttpMessage {

    StatusLine getStatusLine();

    void setStatusLine(StatusLine statusline);

    void setStatusLine(ProtocolVersion ver, int code);

    void setStatusLine(ProtocolVersion ver, int code, String reason);

    void setStatusCode(int code) throws IllegalStateException;

    void setReasonPhrase(String reason) throws IllegalStateException;

    HttpEntity getEntity();

    void setEntity(HttpEntity entity);

    Locale getLocale();

    void setLocale(Locale loc);

}

 

奇怪的是,为什么HttpResponse涉及到Entity,而HttpRequest没有涉及到Entity?很简单,因为有的request没有entity (我的观点)。那么问题又来了,那response都有entity??-- 这我回答不了,逆推过去的话,应该是了。。。囧   还是对HTTP不够理解,回头看一下去。

来看一下常用的HttpRequest实现类

技术分享

在说这些实现类之前,必须先说一下HTTP中的method,其实这个method是form元素的属性。在HTML中,form元素内的表单数据可以被提交到服务器 -- 这里涉及到提交的地址、提交的内容与格式、希望服务器处理提交内容的方式。

method就是用来设置”希望服务器处理提交内容的方式”,其实我个人认为它有两个含义:一个是form提交的内容放在什么地方,一个是告诉服务器怎么解析提交的内容。

 

HTTP standard specification 中规定了一组method:GET, POST, PUT, DELETE 等等。详见 HTTP/1.1 Semantics and Contents

HttpCore中提供与HTTP standard specification中规定的method一致的,从上图你可以看到HttpDelete、HttpPost、HttpPut、HttpPatch、HttpHead、HttpGet、HttpOptions、HttpTrace。从其源代码中可以看到每个类对应的标准文档内容。

例如:

org.apache.http.client.methods
@NotThreadSafe 
public class HttpTrace
extends HttpRequestBase
HTTP TRACE method.
The HTTP TRACE method is defined in section 9.6 of RFC2616:
The TRACE method is used to invoke a remote, application-layer loop- back of the request message. The final recipient of the request SHOULD reflect the message received back to the client as the entity-body of a 200 (OK) response. The final recipient is either the origin server or the first proxy or gateway to receive a Max-Forwards value of zero (0) in the request (see section 14.31). A TRACE request MUST NOT include an entity.
Since:
4.0

所以,当我们需要发送请求时,直接选择相应的类使用即可。

事实上,最常用的只有GET/POST两种方式,所有的请求默认就是GET。

由于POST需要将发送的内容放到HTTP message body也就是entity中,所以 HttpPost类中有entity的GETTER/SETTER。

而GET是将发送的内容作为query string追加到URL后面,所以没有HTTP message body,也就是没有entity。

 

再来看看HttpResponse系列,需要注意的就一个,CloseableHttpResponse 接口,继承了HttpResponse接口和Closeable接口,没有更多内容。

HttpComponents 基础接口/类与HTTP message的对应关系

标签:headers   pat   ptr   define   pac   final   应用   ica   设置   

原文地址:http://www.cnblogs.com/larryzeal/p/6034358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!