zoukankan      html  css  js  c++  java
  • HttpComponents 基础接口/类与HTTP message的对应关系

    前提一:

    什么是HTTP Message -- 遵循HTTP协议发送的消息!其格式是固定的:HTTP Message = Message Line + Message Header + Message Body(optional) 。简言之,Http Message分为Http Request Message 和 Http Response Message两种,简称 Request 和 Response。

    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);
    
    }

    来看一下常用的HttpRequest实现类

    HttpRequestHierarchy

    在说这些实现类之前,必须先说一下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接口,没有更多内容。

  • 相关阅读:
    大话串口:我和它的恩恩怨怨
    分布式网游server的一些想法(一) 语言和平台的选择
    C++: C没有闭包真的很痛苦……
    C++不是C/C++
    最美树算法
    类魔兽世界 技能 天赋 成就 log 系统设计
    C++网游服务端开发(一):又无奈的重复造了个轮子,一个底层网络库
    C++ protobuf 不仅仅是序列化……
    深入WPFStyle
    Illusion = Caliburn.Micro + MEF
  • 原文地址:https://www.cnblogs.com/larryzeal/p/6034358.html
Copyright © 2011-2022 走看看