The difference between HTTP protocol, GET request and POST request

HTTP protocol

What is an agreement?

The agreement is actually a set of norms formulated in advance by some people or a county organization, and everyone follows the norms, so that communication can be barrier-free.

An agreement is a set of norms, a set of standards. Assigned by another person or other organization.

You can understand what I say, and I can understand what you say. This shows that we have a set of norms and a set of agreements. This set of agreements is: the Chinese Mandarin Agreement. There is no barrier to communication between us.

image.png

What is the HTTP protocol?

HTTP protocol: It is a hypertext transfer protocol developed by W3C. (Communication protocol: Template extraction for sending messages is formulated).

W3C :

  • World Wide Web Consortium
  • Responsible for formulating standards: HTTP HTML4.0 HTML5 XML DOMand other specifications are formulated by the W3C.
  • Father of the World Wide Web: Tim Berners-Lee

What is hypertext?

  • Hypertext means: not ordinary text, such as streaming media: sound, video, pictures, etc.
  • HTPP protocol support: not only can transmit ordinary strings, but also support streaming media information such as audio, video, and pictures.

This protocol travels between B and S. B sends data to S to follow the HTTP protocol, and S to send data to B also needs to follow the HTTP protocol, so that B and S can be decoupled.

What is decoupling?

  • B does not depend on S.
  • S does not depend on B either.
  • Coupling means that there are dependencies between objects, reducing coupling and extending software functions
  • B/S means: B/S structure system (a system in which a browser accesses a WEB server)
  • The browser sends data to the web server, called: request (request)
  • The web server sends data to the browser, called a response

HTTP protocols include:

  • Request protocol: When the browser sends data to the WEB server, the sent data needs to follow a set of standards, which specifies the specific format of the sent data.
  • Corresponding protocol: When the WEB server sends data to the browser, the sent data needs to follow a set of standards, which specifies the specific format of the sent data.

The HTTP protocol is a message template that has been extracted and formulated.

  • No matter what brand of browser you are, this is the case.
  • No matter what brand of WEB server you are, this is how it is sent.
  • The FF browser can send requests to Tomcat or to the Jetty server. The browser does not depend on the specific server brand.
  • WEB服务器也不依赖具体的浏览器品牌。可以是FF浏览器,也可以是Chrome浏览器,可以是IE,都行。

HTTP的请求协议包括:4部分

  • 请求行
  • 请求头
  • 空白行
  • 请求体

image.png

HTTP请求协议的具体报文:GET请求

GET /servlet05/getServlet?username=lucy&userpwd=1111 HTTP/1.1 请求行
Host: localhost:8080 请求头
Connection: keep-alive
sec-ch-ua: “Google Chrome”;v=“95”, “Chromium”;v=“95”, “;Not A Brand”;v=“99”
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: “Windows”
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/95.0.4638.54
Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,
application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Referer: http://localhost:8080/servlet05/index.html
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
空白行
请求体
复制代码

HTTP请求协议的具体报文:POST请求

POST /servlet05/postServlet HTTP/1.1 请求行
Host: localhost:8080 请求头
Connection: keep-alive
Content-Length: 25
Cache-Control: max-age=0
sec-ch-ua: “Google Chrome”;v=“95”, “Chromium”;v=“95”, “;Not A Brand”;v=“99”
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: “Windows”
Upgrade-Insecure-Requests: 1
Origin: http://localhost:8080
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36
Accept: 
text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Referer: http://localhost:8080/servlet05/index.html
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
空白行
username=lisi&userpwd=123 请求体
复制代码

请求行

包括三部分:

  • 第一部分:请求方式(7种)
    • get(常用的)
    • post(常用的)
    • delete(常用的)
    • put(常用的)
    • head(常用的)
    • options(常用的)
    • trace(常用的)
  • 第二部分:URI
    • URI:统一资源标识符。代表网络中某个资源的名字。但是通过URI是无法定位资源的。
    • URI和URL什么关系,有什么区别?
  • 第三部分:HTTP版本协议号

请求头

  • 请求的主机
  • 主机的端口
  • 浏览器信息
  • 平台信息
  • cookie信息
  • ....

空白行

  • 空白行是用来区分“请求头”和“请求体”

请求体

  • 向服务器发送的具体数据

HTTP的响应协议(S -- > B)

  • HTTP的相应协议包括:4部分
    • 状态行
    • 响应头
    • 空白行
    • 响应体
  • HTTP响应协议的具体报文:
HTTP/1.1 200 ok                                     状态行
Content-Type: text/html;charset=UTF-8               响应头
Content-Length: 160
Date: Mon, 08 Nov 2021 13:19:32 GMT
Keep-Alive: timeout=20
Connection: keep-alive
                                                    空白行
<!doctype html>                                     响应体
<html>
    <head>
        <title>from get servlet</title>
    </head>
    <body>
        <h1>from get servlet</h1>
    </body>
</html>
复制代码
  • 状态行(三部分组成)
    • 第一部分:协议版本号(HTTP/1.1)
    • 第二部分:状态码(HTTP协议中规定的响应状态号。不同的响应结果对应不同的号码。)
      • 200 表示请求响应成功,正常结束。
      • 400 表示访问的资源不存在,通常是因为要么是你路径写错了,要么是路径写对了,但是服务器中对应的资源并没有启动成功,总之404错误是前端错误。
      • 450 表示前端发送的请求方式与后端请求的处理方法不一致发送:
        • 比如:前端是POST请求,后端的处理方式按照get方法进行处理时,发送405
        • 比如:前端是GET请求,后端的处理方式按照post方法进行处理时,发送405
      • 500 表示服务器端的程序出现了异常。一般会认为是服务器端的错误导致的。
      • 以4开始的,一般是浏览器端的错误导致的。
      • 以5开始的,一般是服务器端的错误导致的。
    • 第三部分:状态的描述信息
      • ok 表示正常成功结束
      • not found 表示资源找不到

菜鸟教程的HTTP状态码详细分类以及说明:www.runoob.com/http/http-s…

  • 响应头
    • 响应的内容类型
    • 响应的内容长度
    • 响应的时间
    • ...
  • 空白行
    • 用来分隔“响应头”和“响应体”的。
  • 响应体
    • 响应体就是响应的正文,这些内容是一个长的字符串,这个字符串被浏览器渲染,解释并执行,最终展示出效果。
  • 怎么查看协议内容?
    • 使用chrome浏览器:F12。然后找到network,通过这个面包可以查看协议的具体内容。

image.png

HTTP协议的特点

  • 1、简单快速:客户向服务器请求服务的时候,只需要传送请求方法和路径,请求方法一般是get和post,因为HTTP协议简单,所以HTTP服务器的程序规模小,通信速度快。
  • 2、灵活:HTTP协议运行传输任意类型的数据对象,传输的类型由Content-Type标记
  • 3、无连接:客户端向服务端发送一次请求,服务端接受后,连接就断开了,无连接表示每一次连接都只处理一个请求,这种方式可以节省传输时间。
    • 举个例子:你打电话给朋友说让他帮忙买一瓶水,然后就把电话挂掉了,这个时候,想到说还要麻烦朋友做什么事情,就再打电话过去。
    • HTTP1.1版本以后,支持可连续连接,通过这种连接就有可能建立一个TCP连接后,发送请求并得到回应,然后接着发送请求并得到回应,通过建立和释放TCP连接的开销分摊到多个请求,对每一个请求来说,因为TCO造成的相对开销被大大降低了,而且还可以发送流水线请求,就是说在发送请求1之后,在回应到来之前就可以发送请求2
  • 4、无状态:HTTP协议是无状态协议,无状态指的是对事务处理没有记忆能力,如果后面想要处理之前的信息的话,就必须重传,这样会导致每一次连接传送的数据量增大

GET请求和POST请求有什么区别?

  • get请求:发送数据的时候,数据会挂在URI的后面,并且在URI后面添加一个“?”,“?”后面是数据。这样会导致发送的数据会显示在浏览器的地址栏上。(get请求在“请求行”上发送数据)
  • post请求:发送数据的时候,在请求体当中发送。不会回显到浏览器的地址栏上,也就是说post发送的数据,在浏览器地址栏上看不到。(post在“请求体”当中发送数据)
  • get请求:只能发送普通的字符串。并且发送的字符串长度有限制,不同的浏览器限制不同。这个没有明确的规范。
  • get请求:无法发送大数据量。
  • post请求:可以发送任何类型的数据,包括普通字符串,流媒体等信息:视频、声音、图片。
  • post请求:可以发送大数据量,理论上没有长度限制。
  • get请求:在W3C中是这样说的:get请求比较适合从服务端获取数据
  • post请求:在W3C中是这样说的:post请求比较适合从服务端传送数据

可能好多人,一看到说get请求会把内容显示在地址栏上,就是不安全的

image.png

GET请求是安全的。

get请求是绝对安全的,为什么?因为get请求只是为了从服务器上获取数据。不会对服务器造成威胁。(get本身是安全,你不要用错了。用错了之后又冤枉人家get不安全,你这样不好(太坏了),那是你自己的问题,不是get请求的问题)

post请求是危险的。

为什么?因为post请求是向服务器提交数据,如果这些数据通过后门的方式进入到服务器当中,服务器是很危险的。另外post是为了提交数据,所以一般情况下拦截请求的时候,大部分会选择拦截(监听)post请求。

注意

我这里说的安全是相对而言,事实上它们都不安全,比如get请求,会把数据显示在url上,但是我们要正确使用就不会出现这种问题。post请求则比较危险。

get请求支持缓存

image.png

  • 任何一个get请求最终的“响应结果”都会被浏览器缓存起来。在浏览器缓存当中:
    • 一个get请求的路径a对应一个资源。
    • 一个get请求的路径b对应一个资源。
    • 一个get请求的路径c对应一个资源。
    • ....
  • 实际上,你只要发送get请求,浏览器做的第一件事都是先从本地浏览器缓存中找,找不到的时候才会去服务器上获取。这种缓存机制目的是为了提高用户的体验。
  • 有没有这样一个需求:我们不希望get请求走缓存,怎么办?怎么避免走缓存?我希望每一次这个get请求都去服务器上找资源,我不想从本地浏览器的缓存中取
  • 只要每一次get请求的请求路径不同即可。

image.png

  • 怎么解决?可以在路径的后面添加一个每时每刻都在变化的“时间戳”,这样,每一次的请求路径都不一样,浏览器就不走缓存了。

post请求不支持缓存(POST是用来修改服务器端的资源的)

  • post是发送数据到服务器端去存储。类似db里的update delete和insert语句的感觉。更新db的意思。数据必须放在数据库,所以一般都得去访问服务器端。
  • post请求之后,服务器“响应的结果”不会被浏览器缓存起来,因为这个缓存没有意义。

GET请求和POST请求如何选择,什么时候使用GET请求,什么时候使用POST请求?

  • 怎么选择GET请求和POST请求呢?衡量标准是什么呢?你这个请求是想获取服务器端的数据,还是想向服务器发送数据,如果你是想从服务器上获取资源,建议使用GET请求,如果是为了向服务器提交数据,建议使用POST请求。
  • 大部分的form表单提交,都是post方式,因为form表单中要填写大量的数据,这些数据是收集用户的信息,一般是需要传给服务器,服务器将这些数据保持或修改等。
  • 如果表单中又敏感信息,还是建议使用post请求,因为get请求会回显铭感信息到浏览器地址栏上。(例如:密码信息)
  • To upload a file, it must be a post request. The data to be passed is not just plain text.
  • In other cases, you can use the get request
  • Regardless of whether you are a get request or a post request, the format of the sent request data is exactly the same, but the location is different, and the format is the same:
    • name=value&name=value&name=value&name=value
    • what is name?
      • Take the form form as an example: the name of the input tag in the form form.
    • what is value?
      • Take the form form as an example: the value of the input tag in the form form.

Summarize

Everyone may have this subjective error, thinking that the get request will display the information in the address bar, so it is not safe, and the post is safe. I think this is a common misunderstanding, in fact, this security is relative.

image.png

Guess you like

Origin juejin.im/post/7078956663440408612