HTTP request message and Response message

Source: Internet
Author: User

First, the HTTP message is text-oriented, each field in the message is a number of ASCII strings, the length of each field is indeterminate. HTTP has two types of messages: Request messages and response messages.

An HTTP request message consists of a request line, a request header (header), a blank line, and 4 parts of the request data, giving the general format of the request message.

Online copy of a picture (reproduced from Huashan big bro):

    

Give a clearer, more clear picture:

The following steps analyze the role of each data section.

1. Request Line

The request line consists of 3 fields of the Request Method field, the URL field, and the HTTP protocol version field, separated by a space.

For example, to open the path http://www.baidu.com/index.php, you can view the request message in Firefox browser as:

get/index.php http/1.1

Therefore, the information obtained by separating the spaces is

(1) Request method: GET

(2) URL Information:/index.php

(3) HTTP protocol version: http/1.1

Type of request method:

1. The Get:get method Get method requires the server to place the URL-positioned resource in the data portion of the response message and send it back to the client. When using the Get method, the request parameter and the corresponding value are appended to the URL, using a question mark ("?" ) represents the end of the URL and the start of the request parameter, the length of the pass parameter is limited, each data is separated by the "&" symbol, the transmitted data can be seen directly in the browser's address bar. Obviously, this is not a good way to transfer private data. Also, because different browser-to-address character restrictions are also different, generally only up to 1024 characters can be recognized, so if you need to transfer large amounts of data, it is not appropriate to use the Get method.

2. The Post:post method encapsulates the request parameter in the HTTP request data, appears as a name/value, can transmit a large amount of data, the Post method does not limit the size of the transmitted data, and does not appear in the URL.

Get and post a each have advantages and disadvantages, get method request data, all the same, the user can copy the path freely shared to other people, the post method does not limit the amount of data requested, often used for form submission. The Post method is more secure than the Get method.

3, HEAD: Just like get, only the server accepts a head request and returns only the response header, and does not send the response content. When we only need to look at the state of a page, the use of head is very efficient, because the content of the page is omitted during transmission. In other words, this method is only used to test the state of the current server.

2. Request the head

User-agent: Browser specific types such as: user-agent:mozilla/5.0 (Windows NT 6.1; rv:17.0) gecko/20100101 firefox/17.0

Accept: What data types are supported by the browser such as: accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-charset: What kind of encoding does the browser use, such as: accept-charset:iso-8859-1

Accept-encoding: The browser supports decoding data compression formats such as: Accept-encoding:gzip, deflate

Accept-language: Browser language environment such as: Accept-language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3

Host: The hostname of the request, which allows multiple domain names to be located in the same IP address as the virtual host. Host:www.baidu.com

Connection: Indicates whether a persistent connection is required. keep-alive/close,http1.1 default is persistent connection, which can take advantage of the persistent connection, when the page contains multiple elements (such as applets, pictures), significantly reducing the time it takes to download. To do this, the servlet needs to send a content-length header in the answer, and the simplest implementation is to write the content to Bytearrayoutputstream first and then calculate its size before formally writing the content. such as: connection:keep-alive

Content-length: Represents the length of the request message body. Content-length must appear for the POST request.

The Content-type:web server tells the browser the type and character set of the object it responds to. For example: content-type:text/html; charset= ' gb2312 '

The Content-encoding:web server indicates what compression method (Gzip,deflate) It uses to compress the objects in the response. Example: Content-encoding:gzip

The Content-language:web server tells the browser the language of the object it responds to.

Cookies: The most common request headers, which each time the browser sends a cookie to the server, allows the server to store a small amount of data on the client.

Referer: Contains a URL from which the user accesses the currently requested page from the page represented by the URL. The server will know which page you are coming from. referer:http://www.baidu.com/

Second, HTTP response messages

The HTTP response message corresponds to the HTTP request message and is divided into three parts.

1, Response Line

2. Response Head

3. Response body

Give an example:

http/: gmtcontent-type:text/html;charset=iso-8859-1122     Wrox homepage !--body goes here-->  

  

  

HTTP response message Common properties:

Cache-control
After the response is output to the client, the service side tells the client how to control the cache of the response content through the header.
Below, the settings allow the client to cache the response content for 3,600 seconds, that is, within 3,600 seconds, if the customer accesses the resource again, directly from the client's cache to return content to the customer, do not get from the server (of course, this function is implemented by the client, the server only through this property prompts the client " Should do ", do not do, or decide on the client, if you are claiming to support the HTTP client, then it should be implemented."

cache-control:max-age=3600

ETag

A message header attribute that represents the version of a server-side resource (such as a page), and if a server resource changes, the ETag changes accordingly. It is a useful complement to Cache-control, which allows the client to "smarter" handle when to fetch resources from the server and when it can return a response directly from the cache.

ETag: "737060cd8c284d8af7ad3082f209582d"

Location

We make the page in ASP. Redirect to a page, in fact, let the client send a request to a page, this need to redirect to the A page URL, in fact, through the response message header Location property to inform the client, such as the next message header properties, will make the client Redire CT to Iteye's homepage:

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

Set-cookie

The server can set the client's cookie, the principle is through this response message header properties.

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

HTTP response body: If the request is an HTML page, then the HTML code is returned. If it is JS is the JS code.

HTTP response headers: While setting cookies, caching, and so on, is set in the response header property.

HTTP response lines: Mainly information such as setting the response status.

HTTP request message and Response message

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.