Http 简明教程

HTTP - Messages

HTTP基于客户端-服务器体系结构模型和无状态请求/响应协议,该协议通过在可靠的TCP/IP连接上交换消息来运行。

HTTP is based on the client-server architecture model and a stateless request/response protocol that operates by exchanging messages across a reliable TCP/IP connection.

HTTP“客户端”是一个程序(Web浏览器或任何其他客户端),用于建立与服务器的连接以发送一个或多个HTTP请求消息。HTTP“服务器”是一个程序(通常是类似Apache Web服务器或Internet信息服务IIS之类的Web服务器),通过发送HTTP响应消息来接受连接以响应HTTP请求。

An HTTP "client" is a program (Web browser or any other client) that establishes a connection to a server for the purpose of sending one or more HTTP request messages. An HTTP "server" is a program ( generally a web server like Apache Web Server or Internet Information Services IIS, etc. ) that accepts connections in order to serve HTTP requests by sending HTTP response messages.

HTTP 使用统一资源标识符 (URI) 来标识给定资源并建立连接。一旦建立了连接, HTTP messages 将会以类似于 Internet 邮件 [RFC5322] 和多用途 Internet 邮件扩展 (MIME) [RFC2045] 所使用的方式传递。这些消息包括从客户端到服务器的 requests 和从服务器到客户端的 responses ,它们将具有以下格式:

HTTP makes use of the Uniform Resource Identifier (URI) to identify a given resource and to establish a connection. Once the connection is established, HTTP messages are passed in a format similar to that used by the Internet mail [RFC5322] and the Multipurpose Internet Mail Extensions (MIME) [RFC2045]. These messages include requests from client to server and responses from server to client which will have the following format:

 HTTP-message   = <Request> | <Response> ; HTTP/1.1 messages

HTTP 请求和 HTTP 响应使用 RFC 822 的通用消息格式来传输所需的数据。此通用消息格式由以下四个项目组成。

HTTP requests and HTTP responses use a generic message format of RFC 822 for transferring the required data. This generic message format consists of the following four items.

A Start-line
Zero or more header fields followed by CRLF
An empty line (i.e., a line with nothing preceding the CRLF)
indicating the end of the header fields
Optionally a message-body

在以下章节中,我们将解释 HTTP 消息中使用的每个实体。

In the following sections, we will explain each of the entities used in an HTTP message.

Message Start-Line

起始行将具有以下通用语法:

A start-line will have the following generic syntax:

start-line = Request-Line | Status-Line

在讨论 HTTP 请求和 HTTP 响应消息时,我们将分别讨论请求行和状态行。现在,让我们看请求和响应情况下的起始行示例:

We will discuss Request-Line and Status-Line while discussing HTTP Request and HTTP Response messages respectively. For now, let’s see the examples of start line in case of request and response:

GET /hello.htm HTTP/1.1     (This is Request-Line sent by the client)

HTTP/1.1 200 OK             (This is Status-Line sent by the server)

Header Fields

HTTP 头字段提供关于请求或响应所需的信息,或关于在消息正文中发送的对象的信息。有四种类型的 HTTP 响应消息头:

HTTP header fields provide required information about the request or response, or about the object sent in the message body. There are four types of HTTP message headers:

  1. General-header: These header fields have general applicability for both request and response messages.

  2. Request-header: These header fields have applicability only for request messages.

  3. Response-header: These header fields have applicability only for response messages.

  4. Entity-header: These header fields define meta information about the entity-body or, if no body is present, about the resource identified by the request.

所有上述标头都遵循相同的通用格式,每个标头字段都包含一个名称,后跟冒号 ( : ) 和字段值,如下所示:

All the above mentioned headers follow the same generic format and each of the header field consists of a name followed by a colon (:) and the field value as follows:

message-header = field-name ":" [ field-value ]

以下是各種標頭欄位的範例:

Following are the examples of various header fields:

User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
Host: www.example.com
Accept-Language: en, mi
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Accept-Ranges: bytes
Content-Length: 51
Vary: Accept-Encoding
Content-Type: text/plain

Message Body

HTTP 消息的消息主体部分是可选的,但如果有消息主体,则将其用于携带与请求或响应相关的主体。如果关联了主体,则通常 Content-TypeContent-Length 标头行指定关联主体的性质。

The message body part is optional for an HTTP message but if it is available, then it is used to carry the entity-body associated with the request or response. If entity body is associated, then usually Content-Type and Content-Length headers lines specify the nature of the body associated.

主体是实际承载 HTTP 请求数据(包括表单数据和上传的数据等)和来自服务器的 HTTP 响应数据(包括文件、图像等)的主体。下面显示了主体内容的示例:

A message body is the one which carries the actual HTTP request data (including form data and uploaded, etc.) and HTTP response data from the server ( including files, images, etc.). Shown below is the simple content of a message body:

<html>
   <body>

      <h1>Hello, World!</h1>

   </body>
</html>

接下来的两章将利用上述概念来准备 HTTP 请求和 HTTP 响应。

Next two chapters will make use of above explained concepts to prepare HTTP Requests and HTTP Responses.