HTTP protocol
1. What is the HTTP protocol
What is an agreement
Agreement refers to the rules agreed by both parties or multiple parties and everyone should abide by
The so-called HTTP protocol refers to the rules that the data sent needs to comply with when communicating between the client and the server
The data in HTTP protocol is also called message
2. HTTP protocol format of the request
The client sends a data call request to the server
The server sends back data to the client to call the response
Requests are divided into GET requests and POST requests
1) GET request
- Request line
- The method of the request is GET
- Requested resource path [+? + request parameters]
- Requested protocol and version number HTTP/1.1
- Request header
- It is composed of key: value. Different key value pairs represent different meanings
The following is the request sent when you click the submit button in useServlet.html
GET /myfirst/contextservlet HTTP/1.1 Host: localhost:8080 Connection: keep-alive sec-ch-ua: "Microsoft Edge";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 Edg/95.0.1020.30 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,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/myfirst/useServlet.html Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6 Cookie: JSESSIONID=AAF5BDFD5ADADA3F3BC4FD7B938BA2D1; Idea-862f61e3=6af5da74-a9f1-4dc4-af02-e10aeb57dad4
- Host: indicates the ip address and port number of the requested server
- Connection: tells the server how to handle the connection request
- Keep alive: tell the server not to close the data returned immediately and keep the connection for a short period of time
- Closed: close now
- User agent: user agent, that is, the information of the browser
- Accept: tells the server the type of data the client can receive
- Referer: indicates the address of the source page of the current request page
- Accept encoding: tells the server the data encoding (compression) format that the client can receive
- Accept language: tells the server the type of language the client can receive
- ZH CN: Chinese
- En US: English American
2) POST request
- Request line
- Request mode POST
- Requested resource path [+? + request parameters]
- Requested protocol and version number HTTP/1.1
- Request header
- It is composed of key: value. Different key value pairs represent different meanings
- Blank line
- Used to split the request header and the request body
- Request body
- Is the data sent to the server
POST /myfirst/hello HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 26 Cache-Control: max-age=0 sec-ch-ua: "Microsoft Edge";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 Edg/95.0.1020.30 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,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/myfirst/useServlet.html Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6 Cookie: JSESSIONID=EB29494F56A4D56E2B59C8177383B578; Idea-862f61e3=6af5da74-a9f1-4dc4-af02-e10aeb57dad4
-
Accept: tells the server the type of data the client can receive
-
Accept language: tells the server the type of language the client can receive
-
Referer: indicates the address of the source page of the current request page
-
User agent: represents the information of the browser
-
Content type: indicates the type of data sent
- application/x-www-form-urlencoded
- Indicates that the submitted data format is: name = value & name = value, and then url encodes it
- url encoding is to convert non English content into:% xx%xx
- multipart/form-data
- It means submitting data to the server in the form of multiple segments (submitting in the form of stream for uploading)
- application/x-www-form-urlencoded
-
Content lengh: indicates the length of the sent data
-
Cache control: indicates how to control the cache
- No cache: no cache
3) Description of common request headers
4) Which are GET requests and which are POST requests
What are the GET requests:
- form tag method=get
- a label
- link tag introduces css
- script tag import js file
- img tag import picture
- iframe introduces html pages
- Enter the address in the browser address bar and press enter
What are the POST requests:
- form label method=post
3. The HTTP protocol format of the response
- Response line
- The protocol and version number of the response
- Response status code
- Response state descriptor
- Response header
- It is composed of key: value. Different key value pairs represent different meanings
- Blank line
- Used to split the request header and the request body
- Responder
- Is the data returned to the client
visit http://localhost:8080/myfirst/useServlet.html?a=123 , that is, this page is requested, so the response body returns this page
HTTP/1.1 200 Accept-Ranges: bytes ETag: W/"367-1635527591401" Last-Modified: Fri, 29 Oct 2021 17:13:11 GMT Content-Type: text/html Content-Length: 367 Date: Fri, 29 Oct 2021 17:17:56 GMT Keep-Alive: timeout=20 Connection: keep-alive <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="http://localhost:8080/myfirst/hello" method="get"> <input type="hidden" name="action" value="login" /> <input type="hidden" name="username" value="root" /> <input type="submit"> </form> </body> </html>
- Server: information representing the server
- Content type: indicates the data type of the response body
- Content length: the length of the response body
- Date: time of request response (Greenwich mean time, with a difference of eight hours)
Description of common response codes
- 200: indicates that the request was successful
- 302: indicates request redirection
- 404: indicates that the request server has received it, but the data you want does not exist (the request address is wrong)
- 500: indicates that the request has been received by the server, but the server has an internal error (code error)
4. MIME type description
MIME is a data type in the HTTP protocol
The full name of MIME is "Multipurpose Internet Mail Extensions"
The format of MIME type is "large type / small type" and corresponds to the extension of a file
file | MIME type |
---|---|
Hypertext markup language text | .html, .htm – txt/html |
Plain text | .txt – text/plain |
Rich text | .rtf – application/rtf |
GIF graphics | .gif – image/gif |
JPEG graphics | .jpeg, .jpg – image/jpeg |
au sound file | .au – audio/basic |
MIDI music files | .mid, .midi – audio/midi |
RealAudio music files | .ra, .ram – audio/x-pn-realaudio |
MPEG file | .mpg, .mpeg – video/mpeg |
AVI file | .avi – video/x-msvideo |
GZIP file | .gz – applicaiton/x-gzip |
TAR file | .tar – application/x-tar |