http request and response message format packet format

Hypertext Transfer Protocol (Hypertext Transfer Protocol, referred to as HTTP) is an application layer protocol. HTTP is a request / response protocol, i.e., a client and server after the connection is established, sends a request to the server; server upon request, to give a corresponding response.

  HTTP request packet

  HTTP request from the line of the request, the request header, the request packet body blank lines and four components, as shown below:

HTTP request line by the request, the request header, the request packet body blank lines and 4 parts

  The following request packet format for simple analysis:

  Request Line: The request line by the method of field, URL field and the HTTP protocol version field three parts, separated by a space between them. Common HTTP request method is GET, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, CONNECT;

  ● GET: When the client from the server to read a resource, use the GET method . GET method requires that the server URL locate resources on the part of the response message back to the client to the server also requests a resource. When using the GET method request and a corresponding parameter value is appended URL, using a question mark ( "?") With the beginning and end request parameters representing the URL, the length of transmission parameters is limited. For example, / index.jsp? Id = 100 & op = bind.

  ● POST: When the client to the server can provide more information about using the POST method , POST method to submit data to the server, such as submitting form data is completed, the data will be submitted to the server for processing. GET is generally used to get / query resource information, POST will be included with the user data is generally used to update the resource information. POST method request parameters encapsulated in the HTTP request data, in the form of name / value can transmit large amounts of data;

  Request header: the header request key / value pairs, one pair per line, and the key value colon ":" separated. Request header information to inform the server about the client request, the typical request header are:

  ● User-Agent: generating a request to the browser type;

  ● Accept: the client may identify the response content type list; asterisk "*" is used according to the type of the packet range, with "* / *" indicates that all pharmaceutically type, with the "type / *" indicates the type of acceptable type All subtypes;

  ● Accept-Language: Client acceptable natural language;

  ● Accept-Encoding: Client pharmaceutically coding compression format;

  ● Accept-Charset: acceptable character sets response;

  ● Host: host name of the request, allowing multiple domain name with an IP address at that virtual host;

  ● connection: connection (or Close Keepalive);

  ● Cookie: is stored in the client-side extension field, belonging to the domain cookie is sent to the server in the same domain name;

  Blank line: after the last request header is a blank line, send carriage return and linefeed, notification server no longer has the following request header;

  Request packet body: GET method request packet body is not used, but the use of the POST process. POST method is suitable for applications requiring customers to fill out the form. Associated with the request packet body is the most commonly used type of package Content-Type and the length packet Content-Length;

  HTTP response message

  OK HTTP response message from the state, in response to the head, and blank lines in response inclusion of four parts, as shown below:

  The following simple analysis of the response message format:

  Line Status: The status line of the HTTP protocol version field, a text description of the status code and the status code of three parts, separated by spaces between them;

  ● status code of three digits, the first digit indicates the type of response, common status codes five categories as follows:

  1xx: means that the server has received the client request, the client may continue to send the request;

  2xx: means that the server request has been successfully received and processed;

  3xx: Redirection means that the server requires the client;

  4xx: represent the client's request illegal content;

  5xx: indicates that the server could not handle a normal request from a client and an unexpected error;

  ● Status Code Description text the following values:

  200 OK: means that the client request was successful;

  400 Bad Request: client request indicates a syntax error, it can not be understood by the server;

  401 Unauthonzed: indicates unauthorized request, the status code must be used with the WWW-Authenticate header field;

  403 Forbidden: indicates server receives the request, but refuse to provide the service, it will generally give a reason for not providing the service in response to the body;

  404 Not Found: requested resource does not exist, e.g., the URL entered incorrect;

  500 Internal Server Error: Indicates that the server unexpected error occurred, making it impossible to complete the client's request;

  503 Service Unavailable: Indicates that the server is not currently able to handle the client's request, after a period of time, the server might return to normal;

  Response header: in response header may include:

  Location: Location response header field receiver for redirecting to a new location. For example: The client requested page has the original location does not exist, in order to allow the client to redirect to a new location this page, the server can use to redirect back to the statement after the Location response header, allowing the client to access the new domain name resources on the corresponding server;

  Server: Server response header field contains a server for processing the request message and the software version. It User-Agent request header field is corresponding to the former transmission information server software, which sends the client software (browser) and the operating system information.

  Vary: header indicates non-cacheable list request;

  Connection: connection;

  Request is: close (Tell  WEB server or the proxy server , upon completion of the response to this request, disconnected, this follow-up without waiting for the connection request). Keepalive (told WEB server or proxy server, in response to the request after completing this, the holding connection, subsequent requests waiting for this connection);

  The response is: close (the connection has been closed); Keepalive (connection maintained, subsequent requests waiting for this connection); Keep-Alive: If the browser request remains connected to the head portion connected to the server indicating a desire to multiple WEB time (in seconds); for example: Keep-Alive: 300;

  WWW-Authenticate: WWW-Authenticate response header field must be included in 401 (unauthorized) response message, the header field and the front mentioned Authorization request header field is related, when the client receives the 401 response message, it to decide whether to request a server to verify it. If the server is required to be verified, it may send a request containing the Authorization header field;

  Blank line: After the last head is a response to a blank line, send carriage return and linefeed, the notification server is no longer responsive to the head.

  Response packet body: the server returns the text information to the client;

  HTTP works

  HTTP protocol using a request / response model. The client sends the server a request packet, the server as a response to a status.

  The following is a step of HTTP request / response:

  ● client to connect to the web server: HTTP client establishes a TCP connection to the web server;

  ● The client sends an HTTP request to the server: through the established TCP connection, the client sends a request message to the server;

  ● The server receives HTTP requests and returns HTTP responses: the server resolution request, the resource location request, the server writes a copy of the TCP connection resource, read by the client terminal;

  ● release the TCP connection: if the connection mode is close, the server closes the TCP connection, the client passive close the connection, releasing the TCP connection; if the connection mode Keepalive, then the connection will be maintained for a period of time, can continue to receive requests within the time ;

  ● client browser parses HTML content: html text client server response parsing and display;

  For example: Type the URL in the browser address bar, then press Enter to go through the following process:

  1, a browser to  a DNS server requesting resolution of the domain name in the URL corresponding to IP addresses;

  2, the IP address parsed TCP connection establishment according to the IP address and the default port 80, and the server;

  3, the browser issues a read file (URL domain name corresponding to the rear portion of the file) of the HTTP request, the request packet as a TCP three-way handshake packets of the third data to the server;

  4, the server responds to the browser request, and sends the html text corresponding to the browser;

  5, releasing the TCP connection;

  6, the browser displays the html and text content;

  HTTP stateless

  HTTP protocol is stateless (stateless). In other words, when accessing a page on the same server with a second client, the server can not know the client had visited, the server can not distinguish between different clients. Stateless feature of HTTP server simplifies the design, makes it easier to support a large number of concurrent server HTTP request.

  HTTP persistent connections

  HTTP1.0 use of non-persistent connections, the main drawback is that the client must establish and maintain a new connection for each object of a pending request, a document that is, each request will have twice the RTT overhead. Because there may be multiple objects on the same page, it may make non-persistent connection to download a page becomes very slow, and this increased the burden on short connection transmission. Keepalive HTTP1.1 use persistent connections, so-called persistent connection, the server is still maintaining this connection for a period of time after transmitting the response data to allow multiple requests and responses exist in the same connection, i.e. in the case of persistent connections, server after transmitting the response does not close the TCP connection, a client may request other objects to continue through this connection.

  Persistent connection HTTP / 1.1 protocol in two ways:

  ● non-pipelined manner: before issuing the next request after the client before a response is received;

  ● pipelined manner: the client can then send a new request message is received before the HTTP response packet;

  Finally, a specific example:

  Remote Address: Request URL: Request Method:GET Status Code:200 OK Request Headers GET / HTTP/1.1 Host: Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4 Cookie: pgv_pvi=2098703360; PHPSESSID=bc7onl0dojbsatscsfv79pds77; pgv_info=ssid=s1454606128; pgv_pvid=926725350; ts_uid=4084753309 Response Header HTTP/1.1 200 OK Server: nginx Date: Mon, 26 Jan 2015 01:09:10 GMT Content-Type: text/html;charset=utf-8 Content-Length: 3631 Connection: keep-alive X-Powered-By: PHP/5.3.10 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Vary: Accept-Encoding Content-Encoding: gzip

  From the request message can know:

  GET / HTTP/1.1

  GET request method represents a read request to obtain data from a web server, / represents the URL path, URL always begins with a /, / home, says, HTTP protocol version last HTTP / 1.1 uses a 1.1 indication; domain name request As follows:


  Response packet as follows:

  HTTP/1.1 200 OK

Original link:

Guess you like