Introduction to the HTTP protocol

Source: Internet
Author: User
Tags ftp protocol

Introduction to HTTP protocol A brief introduction to the five-layer network protocol.
    • Physical layer: To achieve a physical level of bitstream transmission, such as converting 0101 into a voltage signal through the cable transmission. At the bottom of the protocol.
    • Link layer: Information transfer between different MAC addresses within the LAN, and packets will encapsulate Mac information.
    • Network layer: Through IP protocol, the transmission of IP packets (including source IP and destination IP information) between different IP addresses is realized.
    • Transport layer: Built on the IP protocol to achieve communication between different host ports. such as TCP, UDP protocol. TCP transmits the data stream, which guarantees the orderly and reliable transmission of the packet.
    • Application layer: Various network applications will have their own unique communication protocols, such as the Mail Transfer Protocol, FTP protocol, and the HTTP protocol to be discussed in this article.
HTTP protocol

The HTTP protocol, known as the Hypertext Transfer Protocol (hypertext Transfer Protocol), is the communication standard between the client (primarily the browser) and the server (Web site), based on which the ubiquitous World Wide Web is built. HTTP is typically transmitted based on TCP, and the client initiates a request for a resource, which establishes a TCP connection to the server-specified port (default 80), and the service responds to the request. The HTTP protocol specifies the format of the request and response, the request method, and the response status code.

HTTP request

When entering www.baidu.com in the browser address bar, the following request is made (Simplified):

GET / HTTP/1.1Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Encoding: gzip, deflate, brAccept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2Connection: keep-aliveHost: www.baidu.comUser-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0

The request conforms to the following format criteria:

请求行请求头[空行]消息体[可选]

In the request information above, the request line contains three information:

    • Request method, GET
    • Requested resource path-/represents a request to the root of the Web site, usually index.html or index.php, according to the site settings.
    • Protocol---http/1.1 HTTP The first widely used version is 1.0, where 1.1 is used, and the latest 2.0 protocols are also available.

The description of the request header is as follows:

    • Accept informs the server that the client can accept the response content type (content-types).
    • Accept-encoding can accept the encoding method.
    • Accept-language is able to accept a list of natural languages for the response content.
    • Connection the connection type. Keep-alive means to keep the connection for other resources such as CSS, pictures, and other requests to use.
    • Host information, specify which Web site to make a request, except host, the other content in the header information is optional.
    • User-agent the client type. Different browsers are not the same, and the server can return the differentiated content accordingly.

Additional information may also be carried in the request header as needed. List a few more commonly used:

    • Cookies because HTTP is a stateless protocol, cookies are often used to pass on personalized information, such as PHPSESSID, which enables session control to maintain user state.
    • Referer represents the previous page visited by the browser, and it is a link on that page that takes the browser to the currently requested page.
    • If-modified-since corresponds to the last-modified (resource last modified time) in the HTTP response header, and if the server discovers that the resource has not been modified after this time, the 304 not Modified response is returned so that the browser uses the local cache directly. Saves the cost of file transfer.
    • The if-none-match corresponds to the unique identifier of the ETag (Resource version number) file in the HTTP response header, similar to a hash or fingerprint, and the etag changes if the file changes. When the server file ETag is the same as the If-none-match content in the request header, a 304 response is issued, otherwise the new file is returned. The ETag priority is higher than last-modified because the server time may be inaccurate.
    • The range of request resources for range, typically bytes (which can be understood as "paging"). The continuation of the breakpoint can be achieved.

After the request header ends, there is a blank line, followed by an optional message body, used to submit data to the server, such as the form information for the POST request. In this case, the request does not have a message body.

HTTP response

After the request arrives at the Baidu.com server, the following response is issued:

HTTP/1.1 200 OKCache-Control: privateConnection: Keep-AliveContent-Encoding: gzipContent-Type: text/html; charset=utf-8Date: Fri, 30 Mar 2018 07:26:56 GMTExpires: Fri, 30 Mar 2018 07:26:05 GMTServer: BWS/1.1Set-Cookie: BDSVRTM=0; path=/<!DOCTYPE html><!--STATUS OK-->[省略html内容]

Similarly, the response has a certain format:

状态行响应头[空行]响应体

The status line also contains three items of information:

    • http/1.1 Protocol version
    • 200 status code.
    • OK Status Description

The fields in the response header are described as follows:

    • Cache-control cache control, private means that this file is different for different users. Only the user's own browser is able to cache, the intermediary proxy server does not allow caching; No-cache cannot be cached.
    • Connection TCP connection options. The keep-alive maintains and re-uses the connection.
    • Content-encoding content encoding using gzip compression, the browser should be decompressed in the appropriate way.
    • Date when the server responded.
    • Expires Resource Expiration time.
    • Server Software type.
    • Set-cookie set the Cookie.

In addition, there are other more important response headers:

    • Location redirection.
    • ACCESS-CONTROL-ALLOW-ORIGIN specifies which sites can participate in the cross-origin resource sharing process.
    • Last-modified Resource last modified time.
    • Pragma Common value No-cache. HTTP1.0 can use this to disallow caching.
    • Unique identifier of the ETAG resource.

After the response ends, there is a blank line, followed by the body of the response, in this case the HTML content of the Baidu.com home page.

HTTP request method

Note: This section contains a reference to the Chinese wiki

The http/1.1 protocol defines eight methods (also called "actions") to manipulate the specified resources in different ways:

    • GET sends a "show" request to the specified resource. Using the Get method should only be used to read the data, and should not be used in operations that produce "side effects."
    • The HEAD, like the Get method, is a request to the server for the specified resource. Only the server will not return the resources to this section of this article. The advantage of this approach is that it allows you to get "information about this resource" (meta-information or meta-data) without having to transfer the entire content.
    • POST submits data to the specified resource, requesting the server to process it (such as submitting a form or uploading a file). The data is included in the request for this article. This request may create new resources or modify existing resources, or both.
    • PUT uploads its latest content to the specified resource location.
    • The delete request server deletes the resource identified by the Request-uri.
    • TRACE echoes the requests received by the server, primarily for testing or diagnostics.
    • Options This method enables the server to return all HTTP request methods that the resource supports. Use the ' * ' instead of the resource name to send the options request to the Web server to test whether the server function is functioning properly.
    • The Connect http/1.1 protocol is reserved for proxy servers that can change connections to pipelines. Usually a link to an SSL-encrypted server (via a non-encrypted HTTP proxy server).

Method names are case-sensitive. When a request is directed to a resource that does not support the corresponding request method, the server should return the status Code 405 (method not allowed), and should return the status Code 501 (not implemented) when the server does not recognize or support the corresponding request method.

HTTP response Status Code
    • The 1** server receives the request and requires the requestor to continue the operation.
    • 2** succeeds, the operation is successfully received and processed. Typical, such as the OK, in response to all the smooth.
    • 3** Redirect, requires further action to complete the request, such as 304 not Modified, resource unmodified, use your cache.
    • 4** a client error, the request contains a syntax error, or the request cannot be completed. The famous 404 Not Found What you want is not OH.
    • 5** Server error, the server has an error while processing the request. For example, Internal Server error, the server internal errors, probably the programmer is more afraid to see.

Introduction to the HTTP protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.