HTTP
- The HTTP protocol is used for communication between clients and servers
- Communication occurs through requests and responses, with requests sent by the client and responses replied by the server
- Stateless protocol does not save state information. In order to save state information, cookies are introduced
- 1. The client initiates the first request
- 2. The server generates Cookie information and notifies the client to save the Cookie through the set-cookie header field
- 3. The client automatically adds Cookie information to the request packet next time
- 4. The server receives the message and compares the information on the server to determine who the user is
- Persistent connection: Originally HTTP, TCP connections were disconnected for every HTTP communication. HTTP/1.1 introduced keep-alive persistent connections. If either end did not explicitly request disconnection, the TCP connection was saved
- Pipelining: Previously, a request was sent waiting for a response to be received before the next request could be sent. With the introduction of pipelining, multiple requests can be sent simultaneously in parallel without waiting for a response
The HTTP message
- 1. The user only wants to obtain resources. If the GET method is used, the body of the request packet has no content
- 2. When the user sends a file to the server, use the PUT method to send the content in the packet body
- 3. The server returns the requested resource. The body of the response packet contains the requested resource
- 4. When the server returns the 304 status code, it tells the user to use the cache that has not expired. The body of the response packet is empty
Request header
Request method
- GET: Obtains resources
- POST: indicates the body of the transmission request packet, which does not aim to obtain the response
- PUT: transfers files and adds file contents to the request packet body
- HEAD: the same as GET, but does not return the packet body
- DELETE: deletes a file
- OPTIONS: Queries the supported methods of a specified resource
- TRACE: indicates a tracing path
- CONNECT: The tunnel protocol is required to CONNECT to the agent
Response header
Status code
Status code = status code value + Cause phrase
-
Status code category
- 1XX: informational status code ————– The received request is being processed
- 2XX: success status —————- The request is successfully processed
- 3XX: Redirection status code ————– Additional operations are required to complete the request
- 4XX: client error status code ——— The server cannot process the request
- 5XX: Server error status code ——- The server fails to process a request ⚠️ You can change the status code defined in RFC2616 or create the status code by the server as long as you follow the definition of the status code list
-
Common status code
- 200 OK—————————- Indicates that the server processes the request from the client normally. However, different request methods have different results. When the GET method is used, the corresponding request resource is returned as a response. When the HEAD method is used, the corresponding resource does not return the response, but only the packet header. The server passively does not return the packet body
- 204 NO Content——————- The request is processed successfully, but NO resource is returned. The response packet does not contain the packet body, and the server actively does not return the packet body
- 206 Partial Content—————- The client actually performed a successful GET request on the server, but the client requested that data within the specified range be returned
- 301 Moved Permanently Permanently———– The server Permanently allocates a new URI (Uniform resource Identifier) to the resource accessed by the client. In this case, the server adds the Location header field in the response packet, and the client saves the prompted URI. Automatically reschedule the request based on the new URI and get the data
- 302 Found————————- Temporary redirection The server temporarily moves resources, and the server responds with a new URI. Instead of saving the new URI, the client automatically sends requests based on the new URI to obtain data
- 303 See Other———————URI has been updated to explicitly prompt clients to use the GET method to request resources compared to 301,302, which does not specify the client’s next request method, however, In fact, almost all browsers change POST to GET and remove the body of the request message
- 304 Not Modified————– The resource was found, but did Not match the condition sent by the client. This has nothing to do with the redirection, indicating that the client directly used an unexpired cache
- 307 Temporary Redirect—— Temporary Redirect is the same as 302, but 307 does not change the request method
- 400 Bad Request A syntax error exists in the ————- Request
- 401 Unauthorized————- Authentication in the request fails
- 403 Forbidden—————- The server cannot access the resource
- 404 Not Found—————- The resource is Not Found on the server, or the server rejects the request without giving a reason
- 500 Internal Server Error——- An Internal fault occurs on the Server
- 503 Service Unavailable——– The server is temporarily overloaded or is being stopped for maintenance