5. Introduction to http protocol, http request and response
http protocol overview
1. What is HTTP protocol
HTTP is short for hypertext transfer protocol, which transfers HTML files.
HTTP is the most widely used protocol on the Internet. All the protocols beginning with www follow this protocol (possibly https)
2. Role of HTTP protocol
HTTP function: used to define the process of data exchange between WEB browser and WEB server and the content of the data itself
Interaction process between browser and server: browser request, service request response
Request (request line, request header, request body)
Response (response line, response header, response body)
3. Summary
- HTTP protocol: Hypertext Transfer Protocol, which defines the rules when the client and server interact
- The role of HTTP protocol: define the interaction process between client and server and the transmitted data
http request

img
- get mode request
[[request line] GET /myApp/success.html?username=zs&password=123456 HTTP/1.1 [[request header] Accept: text/html, application/xhtml+xml, */* X-HttpWatch-RID: 41723-10011 Referer: http://localhost:8080/myApp/login.html Accept-Language: zh-Hans-CN,zh-Hans;q=0.5 User-Agent: Mozilla/5.0 (MSIE 9.0; qdesk 2.4.1266.203; Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko Accept-Encoding: gzip, deflate Host: localhost:8080 Connection: Keep-Alive Cookie: Idea-b77ddca6=4bc282fe-febf-4fd1-b6c9-72e9e0f381e8
- post request
[[request line] POST /myApp/success.html HTTP/1.1 [[request header] Accept: text/html, application/xhtml+xml, */* X-HttpWatch-RID: 37569-10012 Referer: http://localhost:8080/myApp/login.html Accept-Language: zh-Hans-CN,zh-Hans;q=0.5 User-Agent: Mozilla/5.0 (MSIE 9.0; qdesk 2.4.1266.203; Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate Host: localhost:8080 Content-Length: 27 Connection: Keep-Alive Cache-Control: no-cache [[request body] username=zs&password=123456
1. Request line
- Request line
GET /myApp/success.html?username=zs&password=123456 HTTP/1.1 POST /myApp/success.html HTTP/1.1
- Request mode (8, put,delete, etc.) GET: clear text transmission is unsafe. The parameters follow the request path. There are restrictions on the size of the request parameters, POST: dark text transmission is safer. The request parameters are in the request body. There is no limit on the size of the request parameters,
- URI: uniform resource identifier (i.e. remove the protocol and IP address)
- Protocol version: HTTP/1.1
2. Request header
From line 2 to blank line, it is called request header, which exists in the form of key value pair, but there is a request header with a key corresponding to multiple values
Function: the browser tells the server its own relevant settings
- Accept: the MIME type acceptable to the browser, which tells the server what type of files the client can receive.
- User agent: browser information. (browser type, browser version...)
- Accept charset: the browser uses this header to tell the server which character set it supports
- Content length: indicates the length of the request parameter
- Host: the host and port in the initial URL
- Referrer: where did it come from (which resource was it before), anti-theft chain
- Content type: content type, which tells the server the MIME type of data transmitted by the browser and the type of file transmission, application/x-www-form-urlencoded
- Accept encoding: the data encoding method that the browser can decode, such as gzip
- Connection: indicates whether a persistent connection is required. If the server sees that the value here is "Keep -Alive", or that the request uses HTTP 1.1 (HTTP 1.1 makes a persistent connection by default)
- Cookie: This is one of the most important request header information (session technology, which will be discussed later)
- Date: Date: Mon, 22Aug 2011 01:55:39 GMT request time GMT
3. Requestor
Only when the request method is post can there be a request body, that is, the location of the request parameters (submitted data) in the post request
4. Summary
1. Request line 1. Request mode 2. Request path 2. Request header: It consists of key value pairs 3. Request body: only post A request has a request body, post The request body of the request is used to carry the request parameters
http response

img
Response part
[[response line] HTTP/1.1 200 [[response header] Accept-Ranges: bytes ETag: W/"143-1557909081579" Last-Modified: Wed, 15 May 2019 08:31:21 GMT Content-Type: text/html Content-Length: 143 Date: Sun, 08 Dec 2019 02:20:04 GMT [[responder] <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> Success </body> </html>
1. Response line
HTTP/1.1 200
- Protocol / version
- Response status code

img
200: normal, the connection with the server is successful, and the sending request is successful
302: redirect (jump)
304: read cache indicates that the client cache version is the latest, and the client can continue to use it without requesting from the server
403: insufficient permission for forbidden. The server received the request from the client, but refused to process it
404: the server received the request from the client, but there is no resource you are looking for in my server
500: the server received the request from the client and found the specific resource processing request, but the server made an exception during the processing
2. Response head
The response header exists as key:vaue, and there may be multiple value s. The server instructs the browser to do and configure
- Location: usually used with status code 302 to complete request redirection
- Content type: text / HTML; charset = UTF-8; set the MIME type of the content sent by the server when downloading the file
a.mp3 b.mp4
- Refresh: 5;url=http://www.baidu.com Indicates the refresh frequency of the client. The unit is seconds. eg: tell the browser to jump to Baidu after 5s
- Content disposition: attachment; filename = a.jpg instructs the client (browser) to download files
- Content length: 80 tells the browser the length of the body
- Server: type of Apache Tomcat server
- Content encoding: the encoding type of data sent by gzip server
- Set Cookie: SS = Q0 = 5lb_nq; path = / search cookies sent by the server
- Cache-Control: no-cache (1.1)
- Pragma: no cache (1.0) means to tell the client not to use cache
- Connection:close/Keep-Alive
- Date:Tue, 11 Jul 2000 18:23:51 GMT
3. Responder
The page displays the content, which is the same as the source code of the right-click view of the web page
4. Summary
1. Response line: contains the response status code 1. Common response status codes: 1. 200 OK 2. 302 Redirect redirect 3. 304 Cache Read cache 4. 400 BAD REQUEST There is a problem with the request(It may be that the request parameters do not meet the regulations) 5. 403 Forbidden Reject processing 6. 404 NOT FOUND Resource not found 7. 500 SERVER ERROR Server exception 2. Response header: It consists of multiple key value pairs 3. Responder: 1. It can be used for the display of client pages 2. Available for download