Hyper Text documents contain hyperlinks that point to other resources. Hypertext document is the foundation of World Wide Web (www.
The HTTP protocol solves the problem of file transmission. HTTP is an application layer protocol, mainly based on TCP protocol (occasionally UDP can be used as the underlying layer ). It is popular with the development of the World Wide Web. The HTTP Protocol aims to make better use of the TCP protocol in the World Wide Web environment to transfer files, especially Hypertext files.
Early HTTP mainly transmitted static files, that is, files actually stored on the server. With the development of the World Wide Web, HTTP is used to transmit "dynamic files". programs on the server generate dynamic files based on HTTP requests in real time. The transmission object of HTTP is collectively referred to as resource ).
Order
HTTP enables resource ordering and delivery. It works in a way similar to a fast food order.
Request: the customer sends a request to the waiter: "coming to a chicken burger ".
Response: The waiter responds to the customer's request based on the actual situation.
Depending on the situation, there may be many responses, such:
The waiter prepares a chicken hamburger and delivers it to the customer. (Everything is OK)
The waiter found himself only a dessert site. He asked the customer to open a ticket at the official counter. (Redirection)
The waiter told the customer that the chicken-leg hamburger was gone. (Not found)
After the transaction, the waiter leaves the transaction behind and prepares to serve the next customer.
Next, let's take a look at how HTTP is implemented.
Format
HTTP Communication is a request-responce communication. The client (guest) sends a request to the server and the server (response) Replies to the client.
The HTTP Protocol specifies the request and response formats:
Start line header (headers) Subject (entity body)
The START row has only one row. It contains the most important information for requests/replies. The starting line of the request indicates what the customer wants ". The starting line of the reply indicates "What happened ".
The header information can contain multiple rows. Each row is a key-value pair, for example:
Content-type: text/plain
It indicates that there is a parameter named Content-type, whose value is text/plain. Header information is a supplement to the starting line. The request header information is instructive to the server (it seems that the chicken leg is not spicy ). The reply header prompts the client (for example, note the following on the box: Be careful when hot)
The subject contains specific resources. There is no subject in the request, because we are only placing an order, instead of sending something to the kitchen (the request can have the subject content ). The body of the reply is a text (Hello World !). This text is exactly what the customer expects.
Request
Let's dive into some details. Let's take a look at the request:
GET /index.html HTTP/1.1Host: www.example.com
In the starting line, there are three pieces of information:
GET method. Describes the operations to be performed on the server.
/Index.html resource path. This refers to the index.html file on the server.
HTTP/1.1 protocol version. The first widely used version of HTTP is 1.0, and the current version is 1.1.
In earlier versions, only the GET method was used for HTTP. The server will send specific resources to the customer after receiving the GET request following the HTTP protocol. This is similar to the process in which a customer orders and obtains a hamburger. When the GET method is used, the customer requests resources from the server, so the request usually has no subject part.
The GET method can also be used to transmit unimportant data. It is implemented through URL rewriting. GET data using URL? Variable name = variable value method transmission. For example, send a variable "q" to http: // 127.0.0.1 with the value "". So, the actual URL is http: // 127.0.0.1? Q =. After receiving the request, the server can know that the value of "q" is "".
In addition to the GET method, the most common method is the POST method. It is used to submit data from the client to the server. When the POST method is used, the URL is no longer rewritten. The data is in the body of the http request. The POST method is most used to submit HTML form data. The server often processes the data submitted by the POST method, such as storing the data in the server database.
The sample request contains a line of header information. The header information is named Host. The HTTP request must contain the Host header to describe the server address and port. The default port of the HTTP protocol is 80. If the port is not specified in the HOST, this port is used by default. In this example, the server domain name is www.example.com and port is 80. The domain name is converted to an IP address through the DNS server to determine the IP address of the server on the Internet.