Hypertext Transfer Protocol (HTTP) 应用基本案例
(1)在浏览器(或客户端)中输入一个URL网址,一般以HTTP(http://)或HTTPS(https://)开头;
(2)浏览器基于DNS服务找到目标的IP地址;
(3)浏览器发送HTTP请求,如果无需提供表单数据,那么此时的HTTP请求可以很简单,例如:
GET /index.html HTTP/1.1
Host: www.example.com
(4)HTTP请求经过复杂的网络之后,最终转发到目标主机地址;之后,将提供HTTP响应,最简单的形式如下:
HTTP/1.1 200 OK
(5)若浏览器接收到这个HTTP响应,且其中包含HTML等载荷数据,将进行渲染并显示内容。
另,由于HTTP是一个应用层协议,所以在网络中传输时,存在封装与解封装等涉及网络传输的问题。在此,仅提供应用对等层的相关信息。
HTTP案例
HTTP请求案例 (HTTP Request Examples)
GET /index.html HTTP/1.1
Host: www.example.com
GET / HTTP/1.1
Host: developer.mozilla.org
Accept-Language: fr
POST /contact_form.php HTTP/1.1
Host: developer.mozilla.org
Content-Length: 64
Content-Type: application/x-www-form-urlencoded
name=Joe%20User&request=Send%20me%20one%20of%20your%20catalogue
HTTP响应案例 (HTTP Response Examples)
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 55743
Connection: keep-alive
Cache-Control: s-maxage=300, public, max-age=0
Content-Language: en-US
Date: Thu, 06 Dec 2018 17:37:18 GMT
ETag: "2e77ad1dc6ab0b53a2996dfd4653c1c3"
Server: meinheld/0.6.1
Strict-Transport-Security: max-age=63072000
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Vary: Accept-Encoding,Cookie
Age: 7
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A simple webpage</title>
</head>
<body>
<h1>Simple HTML webpage</h1>
<p>Hello, world!</p>
</body>
</html>
HTTP/1.1 301 Moved Permanently
Server: Apache/2.4.37 (Red Hat)
Content-Type: text/html; charset=utf-8
Date: Thu, 06 Dec 2018 17:33:08 GMT
Location: https://developer.mozilla.org/ (this is the new link to the resource; it is expected that the user-agent will fetch it)
Keep-Alive: timeout=15, max=98
Accept-Ranges: bytes
Via: Moz-Cache-zlb05
Connection: Keep-Alive
Content-Length: 325 (the content contains a default page to display if the user-agent is not able to follow the link)
<!DOCTYPE html>… (contains a site-customized page helping the user to find the missing resource)
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Content-Length: 38217
Connection: keep-alive
Cache-Control: no-cache, no-store, must-revalidate, max-age=0
Content-Language: en-US
Date: Thu, 06 Dec 2018 17:35:13 GMT
Expires: Thu, 06 Dec 2018 17:35:13 GMT
Server: meinheld/0.6.1
Strict-Transport-Security: max-age=63072000
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Vary: Accept-Encoding,Cookie
X-Cache: Error from cloudfront
<!DOCTYPE html>… (contains a site-customized page helping the user to find the missing resource)
HTTP细节
HTTP头部信息 (HTTP Headers)
HTTP头部信息在请求与响应中都存在,以下仅提供重要的头部字段:
Age:对象在代理缓存中的时间(以秒计)。
Connection:网络连接是否存续,如果值为keep-alive,则存续;
Content-Encoding:所用编码格式;
Content-Language:所用语言;
Cache-Control:包含缓存指令;
Expires:超过某时间点响应失效;
Host:设置目标主机与端口号,用于请求;
Server:描述了处理请求的源服务器(即生成响应的服务器)所使用的软件。[重要]
Set-Cookie:用于从服务器向用户代理发送cookie,以便用户代理稍后可以将其发送回服务器。
X-Content-Type-Options:服务器使用的标记,用于指示应遵循Content-Type报头中发布的MIME类型,而不能更改。
X-Forwarded-For:一个事实上的标准报头,用于识别通过代理服务器连接到web服务器的客户端的原始IP地址。[重要]
等等
HTTP请求格式 (HTTP Request Format)
三个部分:
第一部分:即第一行,包含一个请求方法、文件路径(URL)、协议版本;
第二部分:即头部信息(Headers),位于第一行之后至第三部分之前,且在第二部分结束后需要空一行,之后才能提供第三部分内容;
第三部分:数据载荷(Body,可选),主要用于POST方法;
HTTP请求方法 (HTTP Request Methods/Verbs)
CONNECT方法: 启动与所请求资源的双向通信。它可以用来打开隧道。
DELETE方法:请求删除指定的URL资源 。
GET方法:只能用于请求数据(不应包含数据)。
HEAD方法:请求查看URL资源的头部信息。
OPTIONS方法:获取URL资源或服务器所允许的请求方式,可用*表示整个服务器;
PATCH方法:对资源进行部分修改;
POST方法:向服务器发送数据,请求的数据载荷(Body)的数据类型由Content-Type头信息提供;
PUT方法:通过创建新的资源或请求资源替换目标资源;
TRACE方法:用于消息回环测试,即一种调试机制;
HTTP响应格式 (HTTP Response Format)
三个部分:
第一部分:即第一行,为状态行,包含协议版本、响应状态码及状态文本解释;
第二部分:即头部信息(Headers),位于第一行之后至第三部分之前,且在第二部分结束后需要空一行,之后才能提供第三部分内容;
第三部分:返回的数据载荷;
HTTP响应状态码 (HTTP Response Status Codes)
100 Continue
101 Switching Protocols
102 Processing
103 Early Hints
200 OK:请求成功
201 Created:成功,资源已创建
202 Accepted
203 Non-Authoritative Information
204 No Content
205 Reset Content
206 Partial Content
207 Multi-Status
208 Already Reported
226 IM Used
300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other
304 Not Modified
307 Temporary Redirect
308 Permanent Redirect
400 Bad Request
401 Unauthorized
402 Payment Required
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
409 Conflict
410 Gone
411 Length Required
412 Precondition Failed
413 Content Too Large
414 URI Too Long
415 Unsupported Media Type
416 Range Not Satisfiable
417 Expectation Failed
418 I’m a teapot
421 Misdirected Request
422 Unprocessable Content
423 Locked
424 Failed Dependency
425 Too Early
426 Upgrade Required
428 Precondition Required
429 Too Many Requests
431 Request Header Fields Too Large
451 Unavailable For Legal Reasons
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
506 Variant Also Negotiates
507 Insufficient Storage
508 Loop Detected
510 Not Extended
511 Network Authentication Required