HTTP request and response packets

Source: Internet
Author: User

1. Http packets are text-oriented. Each field in the packet is an ASCII string, and the length of each field is uncertain. HTTP has two types of packets: Request Message and Response Message.

An HTTP Request Message consists of four parts: request line, request header, empty line, and request data.

Copy an image online (reproduced from Master Huashan ):

    

Give a clearer and clearer picture:

The following describes the functions of each data part.

1. Request Line

A request line consists of three fields: Request Method Field, URL field, and HTTP Protocol version field, which are separated by spaces.

For example, to open the URL http://www.baidu.com/index.php, you can use a Firefox browser to send the following request message:

Getindex. php HTTP/1.1

Therefore, the information obtained after being separated by spaces is

(1) Request Method: Get

(2) URL Information:/index. php

(3) HTTP Protocol Version: HTTP/1.1

Request Method Type:

1. Get: The get method requires the server to put the URL-located resource in the data part of the Response Message and send it back to the client. When the get method is used, the request parameters and corresponding values are appended to the URL, and a question mark ("?") is used. It indicates the end of the URL and the start of the request parameter. The length of the transmitted parameter is limited. Data is separated by the "&" symbol. The transmitted data is directly displayed in the address bar of the browser. Obviously, this method is not suitable for transmitting private data. In addition, because different browsers have different character restrictions on the address, generally only up to 1024 characters can be identified. Therefore, if you need to transmit a large amount of data, it is not suitable to use the get method.

2. Post: the post method encapsulates the Request Parameters in the HTTP request data in the form of Names/values, and can transmit a large amount of data. The post method has no limit on the size of the transmitted data, it is not displayed in the URL.

The get and post methods have their own advantages and disadvantages. the data requested by the get method is the same. Users can copy the path and share it with others. The post method does not limit the request data volume and is often used for form submission. The post method is more secure than the get method.

3. Head: Like get, the server only returns the response header after receiving the head request, rather than sending the response content. When we only need to view the status of a page, it is very efficient to use the head, because the page content is saved during transmission. That is to say, this method is only used to test the status of the current server.

2. Request Header

User-Agent: The specific browser type, such as: User-Agent: Mozilla/5.0 (Windows NT 6.1; RV: 17.0) Gecko/20100101 Firefox/17.0

Accept: Data Types supported by the browser, such as: accept: text/html, application/XHTML + XML, application/XML; q = 0.9, */*; q = 0.8

Accept-charset: the browser uses which encoding such as: Accept-charset: ISO-8859-1

Accept-encoding: The data compression formats supported by the browser, such as accept-encoding: gzip and deflate.

Accept-language: the language environment of the browser, for example, accept-language ZH-CN, ZH; q = 0.8, en-US; q = 0.5, en; q = 0.3

HOST: the requested host name, which allows multiple domain names to share one IP address, that is, the virtual host. HOST: www.baidu.com

Connection: Indicates whether a persistent connection is required. Keep-alive/Close, http1.1 is a persistent connection by default, it can take advantage of the advantages of persistent connections, when the page contains multiple elements (such as applet, image ), significantly reduce the time required for download. To achieve this, the servlet needs to send a Content-Length header in the response. The simplest method is to write the content into bytearrayoutputstream first, then, calculate the size of the content before writing it. For example, connection: keep-alive

Content-Length: the length of the Request Message Body. Content-Length must appear for post requests.

Content-Type: the Web server informs the browser of the type and character set of the object to be responded. For example, Content-Type: text/html; charset = 'gb2312'

Content-encoding: indicates the compression method (gzip, deflate) used by the Web server to compress the objects in the response. Example: Content-encoding: Gzip

Content-language: the language of the object that the Web server tells the browser to respond.

COOKIE: The most common request header. Each time a browser sends a cookie to the server, the server is allowed to store a small amount of data on the client.

Referer: contains a URL from which you can access the current requested page. The server knows which page you came from. Referer: http://www.baidu.com/

Ii. Http response packets

HTTP response packets correspond to HTTP request packets, which are also divided into three parts.

1. Response line

2. Response Header

3. response body

To an instance:

HTTP/1.1 200 OK // response line date: sat, 31 Dec 2005 23:59:59 gmtcontent-type: text/html; charset = ISO-8859-1Content-Length: 122 <HTML> 

  

  

Common attributes of HTTP response packets:


Cache-control 

After the response is output to the client, the server uses the header to tell the client how to control the cache of the response content.

The following settings allow the client to cache the response content for 3600 seconds, that is, within 3600 seconds, if the customer accesses the resource again, the content is directly returned from the client cache to the customer, do not obtain it from the server. (Of course, this function is implemented by the client. The server only prompts the client through this attribute that "this should be done". If it is not done, it is determined by the client, if it is a client that claims to support HTTP, it should be implemented in this way ).

Cache-control: Max-age = 3600

Etag

A message header attribute that is used to respond to server resources (such as pages). If a server resource changes, the etag changes accordingly. It is a useful supplement to cache-control. It allows the client to intelligently process when resources are to be retrieved from the server and when responses can be returned directly from the cache.

Etag: "737060cd8c284d8af7ad3082f209582d"

Location

In Asp.net, we make the page redirect to a certain a page, in fact, it is to let the client send a request to a page, the URL of the page that requires redirect, in fact, the client is notified through the location attribute of the response packet header. The following packet header attributes will make the client redirect to the iteye homepage:

Location: http://www.google.com.hk

Set-Cookie

The server can set the cookie of the client, which is implemented by the Response Header attribute.

Set-COOKIE: userid = johndoe; max-age = 3600; version = 1

HTTP response body: if an HTML page is requested, the returned HTML code is returned. If Javascript is used, it is the JS Code.

HTTP Response Header: cookie, cache, and other information are set in the Response Header attribute.

HTTP Response line: mainly set the response status and other information.

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.