📅  最后修改于: 2020-12-14 05:20:59             🧑  作者: Mango
在接收并解释了请求消息后,服务器将以HTTP响应消息进行响应:
以下各节说明了HTTP响应消息中使用的每个实体。
状态行由协议版本,后面的数字状态代码及其关联的文本短语组成。元素由空格SP字符分隔。
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
支持HTTP版本1.1的服务器将返回以下版本信息:
HTTP-Version = HTTP/1.1
Status-Code元素是一个三位数的整数,其中Status-Code的第一位定义响应的类别,而后两位不具有任何分类作用。第一位数字有5个值:
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标头字段时,我们将在单独的章节中研究General-header和Entity-header。现在,让我们检查一下Response标头字段。
响应头字段允许服务器传递关于响应的其他信息,这些信息不能放在状态行中。这些头字段提供有关服务器以及对由Request-URI标识的资源的进一步访问的信息。
接受范围
年龄
标签
位置
代理验证
重试后
服务器
变化
WWW认证
如果要编写自己的自定义Web客户端和服务器,则可以引入自定义字段。
现在,我们将它们放在一起形成一个HTTP响应,以请求从在tutorialspoint.com上运行的Web服务器获取hello.htm页面
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
Hello, World!
以下示例显示了HTTP响应消息,其中显示了Web服务器找不到请求的页面时的错误情况:
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
404 Not Found
Not Found
The requested URL /t.html was not found on this server.
以下是HTTP响应消息的示例,该示例显示了当Web服务器在给定的HTTP请求中遇到错误的HTTP版本时的错误情况:
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
400 Bad Request
Bad Request
Your browser sent a request that this server could not understand.
The request line contained invalid characters following the protocol string.