Tutorial on http protocol and http protocol
Summary
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)
Headers)
Entity)
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 (key-valuepair), 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.1
Host: 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.
Reply
After receiving the request, the server generates a response corresponding to the request according to the program, for example:
HTTP/1.1200 OK
Content-type: text/plain
Content-length: 12
HelloWorld!
The start line of the reply also contains three pieces of information.
HTTP/1.1 protocol version
Status code 200 ).
OK status description
OK is a text description of Status Code 200, which is only for human reading. The computer only cares about three status codes, namely 200. 200 indicates that everything is OK and the resource returns normally. The status code represents the type of the server response action.
Other common status codes are as follows:
302, redirect: I don't have the resources you want here, but I know there is another place xxx, you can find it there.
404, cannot be found (not found): I can't find the resources you want, I can't do anything.
(When redirecting, the client can go to xxx to find resources based on the 302 suggestion, or ignore the suggestion .)
Content-type indicates the type of resources contained by the subject. Depending on the type, the client can start different processing programs (such as displaying image files and playing audio files ). Below are some common resources
Text/plain text
Text/html HTML text
Image/jpeg image
Image/gif image
Content-length indicates the length of the body, in bytes.
The body of the response is a common text, that is
Hello World!
Stateless
According to the early HTTP protocol, every request-reponse requires a new TCP connection. The TCP connection is re-established every time, so the server cannot know whether the last request and this request come from the same client. Therefore, HTTP Communication is stateless. The server determines that each request is a brand new request, regardless of whether the request comes from the same address or not.
Imagine a good restaurant and a fast food restaurant. The dining room will know the location of the guests. If a ticket is added, the waiter will know that this is the same as the previous order. Sorry, in a fast food restaurant, the waiter does not record the characteristics of the guests. Want to order again? Please requeue ......
With the development of the HTTP protocol, the HTTP protocol allows reuse of TCP connections to save time for establishing connections. However, HTTP remains stateless.
Summary
The HTTP protocol implements resource transmission on the World Wide Web and uses the request-response method.
What is the HTTP protocol?
The HTTP protocol defines how files are transmitted between the server and the client. Currently, the HTTP protocol version is Http1.1. RFC 2616 describes the details of the HTTP protocol.
This Protocol has become a standard between browsers and Web sites.
When I access the Internet, how does the underlying layer interact?
When a visitor clicks a hyperlink, a URL address is submitted to the browser. Through this URL, the browser will know to link the website and obtain the specific page file (or an image or PDF file ).
The foundation of HTTP is to connect to a server and start to transfer files to the browser.
Basic HTTP transmission process
During http transmission, the client requestor requests a file from the server.
The basic process is:
1. The client connects to a host;
2. The server receives the connection,
3. The client requests a file,
4. the server sends a response.
What is the HTTP protocol?
The HTTP protocol defines how files are transmitted between the server and the client. Currently, the HTTP protocol version is Http1.1. RFC 2616 describes the details of the HTTP protocol.
This Protocol has become a standard between browsers and Web sites.
When I access the Internet, how does the underlying layer interact?
When a visitor clicks a hyperlink, a URL address is submitted to the browser. Through this URL, the browser will know to link the website and obtain the specific page file (or an image or PDF file ).
The foundation of HTTP is to connect to a server and start to transfer files to the browser.
Basic HTTP transmission process
During http transmission, the client requestor requests a file from the server.
The basic process is:
1. The client connects to a host;
2. The server receives the connection,
3. The client requests a file,
4. the server sends a response.