HTTP (Hypertext Transfer Protocol) is the most widely used network transmission protocol on the Internet. It runs on TCP. The browser, as an HTTP client, sends requests to the HTTP server through URL, and the web server sends response information to the client according to the received requests.
Simple and fast
When the client requests service from the server, it only needs to transfer the request method and path to get the server’s response quickly.
Each connection only processes one request. After the server processes the client’s request and receives the client’s response, it disconnects.
URL (uniform resource locator) is a kind of addressing method specially set for identifying the location of resources on the Internet. Resource locator is a concise representation of the location of resources that can be obtained from the Internet. It is the address of standard resources on the Internet. It consists of: < protocol > / < host >: < port > / < Path >
HTTP request includes four parts: request line, request header, empty line and request data.
In the browser request to open a web page, grab the data
GET /test/index.html HTTP/1.1 Host: www.test.cn Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
Get is the request type, / test / index.html is the resource to be accessed, and HTTP / 1.1 is the protocol version
The request header starts from the second line, and the host indicates the destination of the request (host domain name); User agent is the information of the client, which is the important information to detect the browser type. It is defined by the browser and sent automatically in each request.
Accept tells the server the recognized file format supported by the browser
Accept encoding tells the server the type of encoding the browser supports, usually some compression algorithm
There must be a blank line after the request header
The requested data is also called request body, and any other data can be added. The request body of this example is empty.
The response message consists of four parts
HTTP version number, status code and status message
Date: the date and time when the response was generated; Content type: Specifies HTML (text / HTML) of MIME type, encoding type is UTF-8, content length: content length of response entity
Blank lines are required, as in the request message
The server returns the HTML code to the client
HTTP request method
HTTP request methods are used to operate specified resources in different ways (HTTP and post are commonly used, and get, post, delete and put are generally used for restful interfaces). They can be divided into the following types:
1. Get // requests the specified page information and returns the entity body. 2. Head // is similar to a get request, except that there is no specific content in the returned response, which is used to obtain the header 3. Post // submit data to specified resources for processing requests (such as submitting forms or uploading files). The data is contained in the request body. Post requests may result in the creation of new resources and / or modification of existing resources. 4. Put // the data transmitted from the client to the server replaces the content of the specified document. 5. Delete // request the server to delete the specified page. 6. Connect // HTTP / 1.1 protocol is reserved for the proxy server that can change the connection to pipeline mode. 7. Options // allows the client to view the performance of the server. 8. Trace // echo requests received by the server, mainly for testing or diagnosis. 9. Patch // is a supplement to the put method, which is used to locally update known resources.
HTTP status code
HTTP status code is composed of three decimal digits. The first decimal digit defines the type of status code. The latter two digits have no function of classification. There are five types of HTTP status codes.
HTTP is the most widely used protocol on the Internet, and its importance is self-evident. Through this article, we mainly know HTTP and lay a good foundation for the next TCP.