Network protocol HTTP Protocol

Source: Internet
Author: User

Brief introduction

Hypertext Transfer Protocol (hypertext Transfer Protocol, referred to as HTTP) is the application layer protocol. HTTP is a request/response protocol that sends a request to the server after a client has established a connection with the server, and responds to the request after the server has received it.

HTTPRequest message

The HTTP request message consists of 4 parts of the request line, the request header, the blank line, and the request package, as shown in:


The following is a simple analysis of the request message format:

  1. Request Line: The request line is determined by the method field,URLFields andHTTPThe Protocol version field consists of 3 sections, separated by a space between them. Used inHTTPThe request method hasGET, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, CONNECT;
      • GET : When the client wants to read the document from the server, use the get method. get method requires the server to url The positioned resource is placed in the data portion of the response message and is sent back to the client. When using the get method, the request parameter and the corresponding value are appended to the   URL  , followed by a question mark ("?" ) represents the end of the url and the start of the request parameter, which is limited by the length of the pass parameter. For example, /index.jsp?id=100&op=bind.
      • POST : You can use post method. post method encapsulates request parameters in http in the request data, in the form of a name/value, you can transfer large amounts of data;
  2. Request Header : The request header consists of a keyword/value pair, one pair per line, a keyword and a value separated by the English colon ":". The request header notifies the server that there is information about the client request, and the typical request headers are:
    • user-agent: The type of browser that generated the request;
    • Accept: A list of content types that the client can identify;
    • Host: The hostname of the request, allowing multiple domain names to be located in the same IP address, i.e., the virtual host;
    • connection: Connection type (close or keepalive);
  3. Blank line : After the last request header is a blank line, send a carriage return and newline character, notify the server no longer have the request header;
  4. Request Package Body : The request package body is not used in the GET method, but is used in the POST method. The POST method is useful for situations where a customer needs to fill out a form. The most commonly used request headers associated with the request package are Content-type and content-length;
HTTPResponse message

An HTTP response message consists of a status line, a response header, a blank line, and a response packet body of 4 parts, as shown in:


The following is a simple analysis of the response message format:

  1. Status Line: The status line is determined by the HTTP Protocol version field、Status CodeAnddescription text for the status code3 parts, separated by a space between them;
    • The status code consists of three digits, the first digit indicates the type of response, and the commonly used status code has five main classes as follows:
      • 1xx: Indicates that the server has received the client request, the client can continue to send the request;
      • 2xx: Indicates that the server has successfully received and processed the request;
      • 3xx: Indicates that the server requires client redirection;
      • 4xx: Indicates that the client's request has illegal content;
      • 5xx: An unexpected error occurred when the server failed to process the client's request properly;
    • The status Code description text has the following values:
      • OK: Indicates the client request was successful;
      • Bad Request: Indicates that the client requests have syntax errors and cannot be understood by the server;
      • 401 unauthonzed: Indicates that the request was not authorized and that the status code must be used with the www-authenticate header domain;
      • 403 Forbidden: Indicates that the server received the request, but refused to provide the service, usually in the response body given the reasons for not providing services;
      • 404 Not Found: The requested resource does not exist, for example, the wrong URLwas entered;
      • Internal ServerError: Indicates that the server has unexpected errors, resulting in the inability to complete the client's request;
      • 503 Service Unavailable: Indicates that the server is currently not able to process the client's request, after a period of time, the server may return to normal;
  2. Response Head: The response header may include:
    • Location : The Address response header field is used to redirect the recipient to a new position. For example: the client requested the page no longer exists in the original location, in order to redirect the client to the new location of the page, the server can send back to the address of the response header after the use of redirection statements, so that the client to access the new domain name of the corresponding server resources;
    • Server: The server Response header field contains the software information that the server uses to process the request. It corresponds to the user-agent request header domain, which sends information about the server-side software, which sends the client software (browser) and the operating system.
    • connection : Connection;
      • for the request: close (Tell web server or proxy server, After the response of this request has been completed, the connection is disconnected without waiting for subsequent requests for this connection. keepalive (tells the Web server or proxy server, after completing the response of this request, remain connected, waiting for subsequent requests for this connection);
      • for response: close (connection closed); keepalive (connection maintained, waiting for subsequent requests for this connection); keep-alive : If the browser request remains connected, the header indicates that you want to web How long the server remains connected (in seconds), for example: keep-alive:300 ;
    • www-authenticate: Thewww-authenticate response Header field must be included in the 401 (Unauthorized) response message, the header field and the preceding The Authorization request header domain is relevant, and when the client receives the 401 Response message, it decides whether to request the server to validate it. If the server is required to verify it, it can send a request containing the Authorization header domain;
  3. Blank line : The last response header is followed by a blank line that sends a carriage return and a newline character, notifying the server that the following no longer has a response header.
  4. Response Inclusion : text information returned by the server to the client;
HTTPWorking principle

The HTTP protocol uses the request/response model. The client sends a request message to the server, and the server responds with a state. The working model is as follows:

The following are the steps for HTTP request/Response:

  • The client connects to the Web server : TheHTTP client establishes a TCP connection with the Web server;
  • The client initiates an HTTP request to the server : The client sends a request message to the server through an established TCP connection;
  • The server receives an HTTP request and returns an HTTP response : The server resolves the request, locates the request resource, and the server writes the resource copy to the TCP connection, which is read by the client;
  • Release TCP connection : If connection mode is close, the server actively shuts down the TCP connection, the client shuts down the connection passively, releases the TCP connection If the connection mode is keepalive, the connection will remain for a certain period of time, and the request can continue to be received within that time;
  • The client browser parses the HTML content : The client parses and displays the HTML text of the server response;

For example: Type the URLin the browser address bar and press ENTER to experience the following process:

    1. browser to dns server request resolves the ip address for domain names in "font-family:times new Roman" >url ;
    2. resolves the ip address, based on the ip address and default port 80 , and server established tcp connection;
    3. Browser issued read file (HTTP  request for new Roman ">URL  in the latter part of the domain name). The request message is  TCP  the data for the third message of the three-time handshake is sent to the server;
    4. The server responds to the browser request and sends the corresponding html resulting to the browser;
    5. release tcp connection;
    6. browser html Text and display the content;
HTTPNon-stateful nature of the Protocol

The HTTP protocol is stateless (stateless). That is, when the same client accesses a page on the same server for the second time, the server cannot know that the client has visited and the server cannot distinguish between different clients. The stateless nature of http simplifies the design of servers, making it easier for servers to support a large number of concurrent HTTP requests.

Persistent connections

HTTP1.0 uses a non-persistent connection, the main drawback is that the client must establish and maintain a new connection for each object to be requested, that is, twice times the cost of the RTT per request for a document. Because there may be multiple objects on the same page, non-persistent connections can make a page download very slow, and this short connection increases the burden of network transmission. HTTP1.1 Use persistent connection keepalive, the so-called persistent connection, that is, the server after sending the response still for a period of time to maintain the connection, allowing multiple data requests and responses in the same connection, that is, in the case of persistent connection, The server does not close the TCP connection after sending the response, and the client can continue to request other objects through this connection.

There are two ways to http/1.1 A persistent connection to a protocol:

    • Non-pipelined mode: The customer receives the previous response before issuing the next request;
    • Pipelining: The customer can then send a new request message before receiving the HTTP response message;
Finally, a specific example is given:
Remote address:116.57.254.104:80request url:http://hr.tencent.com/request method:getstatus Code:200 OKRequest headersget/http/1.1host:hr.tencent.comconnection:keep-aliveaccept:text/html,application/xhtml+xml,application/ xml;q=0.9,image/webp,*/*;q=0.8user-agent:mozilla/5.0 (X11; Linux i686) applewebkit/537.36 (khtml, like Gecko) chrome/35.0.1916.114 safari/537.36accept-encoding:gzip,deflate, sdchaccept-language:en-us,en;q=0.8,zh-cn;q=0.6,zh;q=0.4cookie:pgv_pvi=2098703360; phpsessid=bc7onl0dojbsatscsfv79pds77; pgv_info=ssid=s1454606128; pgv_pvid=926725350; Ts_uid=4084753309response headerhttp/1.1 OKServer:nginxDate:Mon, 01:09:10 gmtcontent-type:text/html;ch Arset=utf-8content-length:3631connection:keep-alivex-powered-by:php/5.3.10expires:thu, 1981 08:52:00 Gmtcache-control:no-store, No-cache, Must-revalidate, post-check=0, Pre-check=0pragma:no-cachevary: Accept-encodingcontent-encoding:gzip
From the request message you can know:
get/http/1.1
Request methodGETRepresents a read request that will obtain Web page data from the server,/SaidURLThe path,URLAlways with/Beginning/ It means the homepage, the lasthttp/1.1Indicates the use of theHTTPThe protocol version is1.1The request domain name is as follows:
Host:hr.tencent.com
The response message is as follows:
http/1.1 Okserver:nginx



Network protocol 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.