This is my second article on getting started

The definition of Http

HTTP is short for Hyper Text Transfer Protocol, which is used to Transfer hypertext from the World Wide Web server to the local browser. HTTP is based on TCP/IP protocol communication protocol to transfer data (HTML files, image files, query results, etc.). It does not involve packet transmission and mainly defines the communication format between the client and the server. Port 80 is used by default.

The characteristics of Http

Connectionless: HTTP is connectionless, that is, no HTTP connection is required before exchanging HTTP packets

Stateless: The HTTP protocol is stateless: no history or status information is saved during data transmission. The stateless feature simplifies server design, making it easier for the server to support large numbers of concurrent HTPP requests. High transmission reliability: TCP is used as the transport layer protocol (connection-oriented and reliable transport), that is, TCP connections must be established before packets are exchanged

Good compatibility: support B/S mode and C/S mode;

Simple and fast: when a client requests services from the server, it only needs to send the request method and path. The commonly used request methods are GET, HEAD and POST

Flexibility: HTTP allows the transfer of any type of data object

The Http message

HTTP packets are divided into request packets and response packets. A request packet consists of a request line, a header, a blank line, and a request body. The response message consists of status line, response header, blank line and response body. Request line: declares the request message, host domain name, resource path, and protocol version. Request Header: Indicates some information about the client, server, or packet. Request body: stores the data to be sent to the server

HTTP request methods

GET requests the specified page information and returns the entity body.

A HEAD is similar to a GET request, except that there is no concrete content in the response returned to retrieve the header

POST Submits data to a specified resource for processing requests (such as submitting a form or uploading a file). The data is contained in the request body.

PUT Transmits data from the client to the server instead of the content of the specified document.

DELETE requests the server to DELETE the specified page.

Differences between GET and POST requests GET requests

GET /users/? Sex =man&name=Professional HTTP/1.1 Host: www.juejin.cn user-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; Rv :1.7.6) Gecko/20210707 Firefox/1.0.1 Connection: keep-aliveCopy the code

Notice that the last line is empty

A POST request

POST/HTTP/1.1 Host: www.juejin.cn user-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; Rv :1.7.6) Gecko/20210707 Firefox/1.0.1 Content-Type: Application/X-www-form-urlencoded content-Length: 40 Connection: 1.7.6) Gecko/20210707 Firefox/1.0.1 Content-Type: Application/X-www-form-urlencoded content-Length: 40 Connection: Keep-Alive name=Professional%20Ajax&publisher=WileyCopy the code

The Http status code

1XX: indicates an information notification, such as that the request was received or is being processed

2xx: indicates success, such as acceptance or knowledge

3XX: indicates a redirect and further action must be taken to complete the request

4xx: Customer error, such as incorrect syntax or incomplete request: 404

5XX: indicates a server error. For example, the server fails to complete the request

How HTTP works

The HTTP protocol defines how a Web client requests a Web page from a Web server and how the server delivers the Web page to the client. The HTTP protocol uses a request/response model. The client sends a request packet to the server containing the request method, URL, protocol version, request header, and request data. The server responds with a status line containing the protocol version, success or error code, server information, response headers, and response data.

Here are the steps for an HTTP request/response:

1. The client connects to the Web server

An HTTP client, typically a browser, establishes a TCP socket connection with the HTTP port of the Web server (default: 80). For example, www.juejin.cn.

2. Send HTTP requests

Through the TCP socket, the client sends a text request packet to the Web server. A request packet consists of the request line, the request header, the blank line, and the request data.

3. The server accepts the request and returns an HTTP response

The Web server parses the request and locates the requested resource. The server writes the resource copy to the TCP socket, which is read by the client. A response consists of a status line, a response header, a blank line, and response data.

4. Release the TCP connection

If the connection mode is set to close, the server actively closes the TCP connection, and the client passively closes the connection to release the TCP connection. If the Connection mode is Keepalive, the connection is kept for a period of time, during which requests can be received.

5. The client browser parses the HTML content

The client browser first parses the status line to see the status code indicating whether the request was successful. Each response header is then parsed, and the response header tells the following several bytes of HTML document and the document’s character set. The client browser reads the response data HTML, formats it according to the HTML syntax, and displays it in the browser window.