Home >Backend Development >PHP Tutorial >Detailed explanation of HTTP protocol examples
HTTP is an application layer protocol, consisting of requests and responses, and is a standard client service model. HTTP is usually carried on the TCP protocol, and sometimes on the TLS or SSL protocol layer. At this time, it becomes commonly referred to as HTTPS. The default port number for HTTP is 80 and the port number for HTTPS is 443.
The disadvantage is that the server will not actively PUSH to the client
The application of the SPDY protocol requires a client browser and server Both terminals are supported at the same time. Optimizes the communication between the browser and the server, supports stream multiplexing, prioritizes requests, actively initiates requests, forces SSL secure transmission and other advanced features
4.1.2 How the HTTP protocol works
Browsing the web is the main application of the HTTP protocol. An HTTP operation is called a transaction
1 The client needs to establish a connection with the server, click a hyperlink, and the HTTP protocol The work begins.
2 After establishing the connection, the client sends a request to the server.
3 After receiving the request, the server gives the corresponding response information
4 The client receives the information returned by the server and displays it on the user's display screen, and then the client disconnects from the server
Some main concepts in HTTP protocol
1 Request
HTTP request consists of three parts: request header, request header, request body
Request line: Method Request-URL HTTP-Version CRLF
Method: Request method (GET, POST, HEAD, PUT, DELETE, TRACE, CONNECT, OPTIONS)
Request URL: A uniform resource identifier
HTTP Version: The requested HTTP protocol version
CRLF: Carriage return and line feed
2 Response
HTTP response also consists of three parts: status line, message header, response body
Status line: HTTP-Version Status-Code Reason-Phrase CRLF
HTTP Version: Server HTTP protocol version
Status-Code: Response status code sent back by the server
Reason Phrase: Text description of the status code
The status code consists of three digits and five possible values
1XX: Indication information--the request has been received , continue processing
2XX: Success--the request has been successfully received, understood and accepted
3XX: Redirect--further operations must be performed to complete the request
4XX: Client error--The request has a syntax error or the request cannot be fulfilled
5XX: Server error--The server failed to fulfill a legal request
Common status codes and status descriptions
200 OK: The client request is successful
400 bad Request: The client request has a syntax error and cannot be processed What the server understands
401 Unauthorize: The request is unauthorized, this status code must be used with the WWW Authenticate header
403 Forbidden: The server received the request, but refused to provide it Service
404 Not found: The requested resource does not exist, for example, the wrong URL was entered
500 internal Server Error: An unexpected error occurred on the server
503 Server Unavailable: The server is currently unable to process the client's request and may return to normal after a period of time
3 Headers
HTTP message headers include ordinary headers, Request header, response header, entity header
1 There are a few header fields in the ordinary header that are used for all request and response messages, but are not used for the transmitted entity, only for the transmitted message ( Cache control, connection control)
2 The request header allows the client to pass additional information of the request to the server as well as the client's own information (UA header, Accept)
3 The response header allows the server to pass additional response information that cannot be placed in the status line, as well as information about the server and information (Location) for next access to the resource identified by the Request URL
4 The entity header defines meta-information about the entity body and the resource identified by the request, such as whether there is an entity body
Several important headers:
Host The :header field specifies the Internet host and port number of the requested resource, and must indicate the location of the original server or gateway of the requested URL.
User Agent: UA for short, the content contains the user information that made the request. Usually contains the browser's information, mainly the name, version of the browser and the operating system used
Accept: Tells the server what file formats are acceptable. Usually this value is similar across browsers.
Cookie: There are two types: one is sent by the client to the server using the Cookie header, which can have multiple values; the other is sent by the server to the browser, with the header Set Cookie. There can only be one value, you need to specify domain, path
Cache Control: Specify the caching mechanism followed by the request and response. Setting Cache Control in a request message or response message does not modify the cache processing process in another message. Caching instructions when requesting include no cache, no store, max age, max state, no transform, must revalidate, proxy revalidate, max age
Referer: The header field allows the client to specify the source resource of the request URL Address, which allows the server to generate a fallback list for login and cache optimization. Referer is usually a parameter used by the traffic statistics system to record the visitor's address
Content Length: Content length
Content Range: The resource range of the response. The requested resource range can be marked in each request. When the connection is disconnected and reconnected, the client only requests the undownloaded part of the resource instead of re-requesting the entire resource to implement breakpoint resumption. This is the principle of Xunlei, which uses multi-threading to read the resources of silly girls on the Internet in sections, and finally merges them
Accept Encoding: Specifies the encoding method that can be received
Custom Headers: In http messages, you can also use some header fields that are not defined in the formal specifications in http1.1.
Related recommendations:
Summary of HTTP protocol usage used in the header in php
In-depth analysis of HTTP protocol
Introduction and in-depth understanding of HTTP protocol
The above is the detailed content of Detailed explanation of HTTP protocol examples. For more information, please follow other related articles on the PHP Chinese website!