前提一:

什么是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实现类

在说这些实现类之前,必须先说一下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的对应关系的更多相关文章

  1. python开发面向对象基础:接口类&抽象类&多态&钻石继承

    一,接口类 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface,接口类中定义了一些接口名(就是函数名)且并未实 ...

  2. java基础2.-------interface接口类,实现接口

    1.为什么使用接口,是把功能方法都写在一个类中,在其他需要调用的时候,通过implements实现该接口 2.接口可以继承多个父类接口,在实现接口的时候,实现类实现所有方法 3.在接口类写方法时,自动 ...

  3. python基础 抽象类(接口类)

    Python中没有接口.接口类,抽象类:定义 制定一个规范 #必须要导入from abc import ABCMeta,abstractmethod class Payment(metaclass = ...

  4. oc中的oop基础及类的基本介绍

    面向对象的(OOP)的基础知识 类(class):表示一组对象数据的结构体,对象通类来得到自身.类名首字母大写. 对象(objcet):是一种包含值和指向其类的隐藏指针的结构体.运行中的程序中通常会有 ...

  5. springMVC基础controller类

    此文章是基于 搭建SpringMVC+Spring+Hibernate平台 功能:设置请求.响应对象:session.cookie操作:ajax访问返回json数据: 创建springMVC基础con ...

  6. hibernate基础dao类

    此文章是基于 搭建SpringMVC+Spring+Hibernate平台 功能:数据库的保存.更新.删除:sql.hql查询:分页查询:调用存储过程 创建hibernate基础dao类: BaseD ...

  7. python基础——定制类

    python基础——定制类 看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方 ...

  8. .net微信公众号开发——基础接口

    作者:王先荣    本文讲述微信公众号开发中基础接口的使用,包括以下内容:    (1)获取许可令牌(AccessToken):    (2)获取微信服务器地址:    (3)上传.下载多媒体文件:  ...

  9. day 25-1 接口类、抽象类、多态

    # 接口类:python 原生不支持# 抽象类:python 原生支持的 接口类 首先我们来看一个支付接口的简单例子 from abc import abstractmethod,ABCMeta #我 ...

随机推荐

  1. C#模拟PrtScn实现截屏

    有了之前的基础知识了解,如今開始实现PrtScn和Alt+PrtScn. 首先新建一个WPF应用程序,命名为PrintscreenAndAltPrintScreen 导入keybd_event方法: ...

  2. centos7 启动httpd的时候为什么显示是这样的

    我输入 service httpd start显示一下内容:Redirecting to /bin/systemctl start httpd.service -------------------- ...

  3. unity5 Orthographic模式相机视截体尺寸计算

    一,通过编辑器中数值计算. 如图,相机为Orthographic模式,其camera size为5.57,是什么含义呢,经过测量,发现视图中视截体的高度是5.57x2. 那么视截体的宽度怎么决定呢? ...

  4. session和cookie的联系

    前提: 一.cookie机制 正统的cookie分发是通过扩展HTTP协议来实现的,服务器通过在HTTP的响应头中加上一行特殊的指示以提示浏览器按照指示生成相应的cookie.然而纯粹的客户端脚本如J ...

  5. Lua脚本语言快速入门手册

    学了两天Lua语言,感叹其短小精悍,上手极快,语法还很舒服,不错!整理下学习过程中经常用到的基础知识,共勉! Lua用法简述 Lua语言是在1993年由巴西一个大学研究小组发明,其设计目标是作为嵌入式 ...

  6. linux perf and tracer ,java Flame Graph

    http://www.brendangregg.com/flamegraphs.html http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/rev/e826 ...

  7. 每日英语:Why 'The Voice' Is China's No. 1 TV Show

    U.S. fans of the hit talent show 'The Voice' may take for granted that its judges sit with their bac ...

  8. LeetCode: Combinations 解题报告

    Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...

  9. LeetCode: Substring with Concatenation of All Words 解题报告

    Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...

  10. 知乎:在卡内基梅隆大学 (Carnegie Mellon University) 就读是怎样一番体验?

    转自:http://www.zhihu.com/question/24295398   知乎 Yu Zhang 知乎搜索 首页 话题 发现 消息 调查类问题名校就读体验修改 在卡内基梅隆大学 (Car ...