HTTP 协议

HTTP 协议构建于 TCP 协议之上,是一个应用层协议。

HTTP 1.x 协议头信息必须是 ASCII 码,后面的数据可以是任何格式(服务器回应的时候,必须告诉客户端,数据是什么格式,Content-Type 字段的作用)。

 

规范把 HTTP 请求分为三个部分:请求行(Request Line)、请求头(Headers)、消息主体(Body)。其中,headers 和 body 是可选的。

Request Line 和 Headers 以 \r\n 分隔,Headers 与 Body 之间有一个空行(两个 \r\n)。

<method> <request-URL> <version>
<headers>

<body>

method 有很多,常见的就是 GET 和 POST。http 1.1 为了表达不同的语义,引入了诸如 DELETE、PATCH 等方法,这种语义不是强制性的。

协议并没有规定 GET 请求不能带 body 。理论上,任何 HTTP 请求都可以带 body 的。代理一般不会缓存 POST 请求的响应内容。

 

HTTP 请求例子:

GET / HTTP/1.1
Host: www.baidu.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5)
Accept: */*

服务器响应

HTTP/1.1 200 OK 
Content-Type: text/plain
Content-Length: 137582
Expires: Thu, 05 Dec 1997 16:00:00 GMT
Last-Modified: Wed, 5 August 1996 15:55:28 GMT
Server: Apache 0.84

<html>
  <body>Hello World</body>
</html>

 

一次 GET 或 POST 请求产生几个 TCP 数据包,这不是 HTTP 协议的范畴,一次 HTTP 请求发送几个 TCP 数据包由操作系统或框架的系统调用决定。

 

Cookie:

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Cookies

https://www.ruanyifeng.com/blog/2019/09/cookie-samesite.html

CORS:

https://blog.csdn.net/f641385712/category_11135672.html

https://www.ruanyifeng.com/blog/2016/04/same-origin-policy.html

https://www.ruanyifeng.com/blog/2016/04/cors.html

Content-Type:https://blog.csdn.net/wangjun5159/article/details/47781443

Server-sent events:

https://developer.mozilla.org/zh-CN/docs/Web/API/Server-sent_events/Using_server-sent_events

https://www.ruanyifeng.com/blog/2017/05/server-sent_events.html


https://www.rfc-editor.org/rfc/rfc2616.html

https://httpwg.org

https://www.ruanyifeng.com/blog/2016/08/http.html

https://coolshell.cn/articles/19840.html

posted @ 2020-07-19 02:05  江湖小小白  阅读(267)  评论(0编辑  收藏  举报