Http 简明教程

HTTP - Responses

在收到并解读请求消息后,服务器会使用 HTTP 响应消息进行响应:

After receiving and interpreting a request message, a server responds with an HTTP response message:

A Status-line
Zero or more header (General|Response|Entity) 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 响应消息中使用的每个实体。

The following sections explain each of the entities used in an HTTP response message.

Message Status-Line

状态行由协议版本后跟一个数字状态代码及其关联的文本短语组成。元素由空格 SP 字符分隔。

A Status-Line consists of the protocol version followed by a numeric status code and its associated textual phrase. The elements are separated by space SP characters.

Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF

HTTP Version

支持 HTTP 1.1 版本的服务器将返回以下版本信息:

A server supporting HTTP version 1.1 will return the following version information:

HTTP-Version = HTTP/1.1

Status Code

状态代码元素是 3 位整数,其中状态码的第一位定义了响应的类型,而最后两位没有分类作用。第一位有 5 个值:

The Status-Code element is a 3-digit integer where first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit:

S.N.

Code and Description

1

*1xx: Informational*It means the request was received and the process is continuing.

2

*2xx: Success*It means the action was successfully received, understood, and accepted.

3

*3xx: Redirection*It means further action must be taken in order to complete the request.

4

*4xx: Client Error*It means the request contains incorrect syntax or cannot be fulfilled.

5

*5xx: Server Error*It means the server failed to fulfill an apparently valid request.

HTTP 状态代码是可扩展的,HTTP 应用程序不需要理解所有已注册状态代码的含义。所有状态代码的列表已在单独的章节中给出,供您参考。

HTTP status codes are extensible and HTTP applications are not required to understand the meaning of all registered status codes. A list of all the status codes has been given in a separate chapter for your reference.

Response Header Fields

当我们学习 HTTP 头字段时,我们将在单独的章节中学习通用头和实体头。现在,让我们检查一下响应头字段是什么。

We will study General-header and Entity-header in a separate chapter when we will learn HTTP header fields. For now, let’s check what Response header fields are.

响应标头字段允许服务器传递有关响应的附加信息,该信息无法放在状态行中。这些标头字段提供了有关服务器的信息,以及对请求 URI 所标识的资源的进一步访问。

The response-header fields allow the server to pass additional information about the response which cannot be placed in the Status- Line. These header fields give information about the server and about further access to the resource identified by the Request-URI.

  1. Accept-Ranges

  2. Age

  3. ETag

  4. Location

  5. Proxy-Authenticate

  6. Retry-After

  7. Server

  8. Vary

  9. WWW-Authenticate

如果你打算编写自己的自定义 Web 客户端和服务器,则可以引入自定义字段。

You can introduce your custom fields in case you are going to write your own custom Web Client and Server.

Examples of Response Message

现在,让我们将它们放在一起,以形成用于从 tutorialspoint.com 上运行的 Web 服务器中获取 hello.htm 页面的 HTTP 响应。

Now let’s put it all together to form an HTTP response for a request to fetch the hello.htm page from the web server running on tutorialspoint.com

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>

以下示例显示了一个 HTTP 响应消息,该消息显示了 Web 服务器找不到请求页面的错误情况:

The following example shows an HTTP response message displaying error condition when the web server could not find the requested page:

HTTP/1.1 404 Not Found
Date: Sun, 18 Oct 2012 10:36:20 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 230
Connection: Closed
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
   <title>404 Not Found</title>
</head>
<body>
   <h1>Not Found</h1>
   <p>The requested URL /t.html was not found on this server.</p>
</body>
</html>

以下是 HTTP 响应消息的示例,该消息显示了 Web 服务器在给定的 HTTP 请求中遇到错误 HTTP 版本时的错误情况:

Following is an example of HTTP response message showing error condition when the web server encountered a wrong HTTP version in the given HTTP request:

HTTP/1.1 400 Bad Request
Date: Sun, 18 Oct 2012 10:36:20 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 230
Content-Type: text/html; charset=iso-8859-1
Connection: Closed
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
   <title>400 Bad Request</title>
</head>
<body>
   <h1>Bad Request</h1>
   <p>Your browser sent a request that this server could not understand.</p>
   <p>The request line contained invalid characters following the protocol string.</p>
</body>
</html>