HTTP request headers and response headers Explanation [turn]

Old ape recent study at the beginning of the relevant knowledge of reptiles, because the old ape background only non-web applications before and found that the knowledge is too scarce that learning is difficult, therefore had to start from the basics of catching up, for knowledge comparison approved the contents of the old ape will be forwarded directly to the internet to find.
The following article about the http header information to explain in great detail, old ape transferred from the following blog:
Original Address:
The blog also forwarded the following blog:
HTTP request header Detailed [turn]

HTTP consists of two parts: the request and the response. When you enter a URL in a Web browser, the browser will be created according to your requirements and send a request that contains the URL input as well as some relevant information with the browser itself. When the server receives this request returns a response that includes information related to the request and the specified URL (if any) data. Until the browser parses the response and show a web page (or other resources) so far.

HTTP request

HTTP request format is as follows:

<blank line>

In the HTTP request, the first row must be a request line (request line), used to illustrate the type of request to access the resource, and the HTTP version. Followed by a header (header) section, for explaining the additional information server to be used. After the header is a blank line, then this can then add any other data [called body (body)].

In HTTP, it defines several request types, usually only GET requests and POST requests we care about. Just enter a URL in a Web browser, the browser will send a GET request to the server based on the URL, obtain and return to tell the server what resource. For GET request shown below:

GET / HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)
Gecko/20050225 Firefox/1.0.1
Connection: Keep-Alive

The first line illustrates the part of the request that the request is a GET request. The second part of the line is a slash (/), the request is used to illustrate the root domain. The last described part of the line using the version of HTTP 1.1 (Another alternative is 1.0). So where is the request to go? This is the content of the second row.

Line 2 is the first request header, HOST. HOST header will indicate the destination of the request. HOST and the upper bound row slash (/), may inform the server request is 1.1 HOST header is only needed, and the original is not required to use version 1.0). The third row contains the header User-Agent, the server-side and client-side scripts have access to it, it is an important basis for the type of browser detection logic. This information is defined by the browser you use (in the present embodiment, Firefox 1.0.1), and each request will be sent automatically. The last line is the first Connection, usually the browser operations to Keep-Alive (of course also be set to other values). Note that there is a blank line after the last header. Even if there is the request body, the blank line is also required.

To send a GET request parameters, you must be extra information appended to the URL itself. The format is similar:

URL? name1 = value1 & name2 = value2 & ... & nameN = valueN

This information is called query string (query string), it will be copied in the request line of the HTTP request, as follows:

GET /books/?name=Professional%20Ajax HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)
Gecko/20050225 Firefox/1.0.1
Connection: Keep-Alive

Note that in order to text "Professional Ajax" as a URL parameter encoding process needs its content, will be replaced by a space 20%, this is called URL encoding (URL encoding), commonly used in many parts of HTTP (JavaScript provides a built-in URL encoding function and a decoding process). "Name - value" (name-value) of a & separated. Most of the server-side technology to automatically decode the request body, and to provide some way to access these logical values. Of course, how that data is decided by the server.

On the other hand, POST request to provide additional information to the server in the request body. Usually, when filling out an online form and submit it, these data will be filled in to the server POST requests.

The following is a typical POST request:

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)
Gecko/20050225 Firefox/1.0.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 40
Connection: Keep-Alive


It is found from the above, there are some differences between the GET request and the POST request. First, GET request at the beginning of the line to the POST, to indicate the different request types. You will find the header User-Agent Host and still exists, in the back there are two new line. Content-Type header which describes the content of the request body is coded. Browser always application / format encoding x-www-form- urlencoded to transmit data, which is the MIME type for simple URL-encoded. Content-Length header describes the number of bytes requested body. After the Connection header is a blank line, and then later request that the body. POST request with most browsers, like, this is a simple "name - value" of the form given, where name is Professional Ajax, publisher is Wiley. You can be organized in the same format of the URL query string parameters.

Here are some of the most common request header:

Accept - Charset:浏览器可接受的字符集。
Accept - Encoding:浏览器能够进行解码的数据编码方式,比如gzip。Servlet能够向支持gzip的浏览器返回经gzip编码的HTML页面。许多情形下这可以减少5到10倍的下载时间。
Accept - Language:浏览器所希望的语言种类,当服务器能够提供一种以上的语言版本时要用到。
Authorization:授权信息,通常出现在对服务器发送的WWW - Authenticate头的应答中。
Connection:表示是否需要持久连接。如果Servlet看到这里的值为“Keep - Alive”,或者看到请求使用的是HTTP 1.1(HTTP 1.1默认进行持久连接),它就可以利用持久连接的优点,当页面包含多个元素时(例如Applet,图片),显著地减少下载所需要的时间。要实现这一点,Servlet需要在应答中发送一个Content - Length头,最简单的实现方法是:先把内容写入ByteArrayOutputStream,然后在正式写出内容之前计算它的大小。
Content - Length:表示请求消息正文的长度。
If - Modified - Since:只有当所请求的内容在指定的日期之后又经过修改才返回它,否则返回304“Not Modified”应答。
Pragma:指定“no - cache”值表示服务器必须返回一个刷新后的文档,即使它是代理服务器而且已经有了页面的本地拷贝。
User - Agent:浏览器类型,如果Servlet返回的内容与浏览器类型有关则该值非常有用。
UA - Pixels,UA - Color,UA - OS,UA - CPU:由某些版本的IE浏览器所发送的非标准的请求头,表示屏幕大小、颜色深度、操作系统和CPU类型。

HTTP response

As shown below, the request format as the HTTP response is very similar:

<blank line>

As you can see, the only real difference in the response that the status information by the first row in place of the request information. Status line (status line) by providing a status code to illustrate the requested resources. The following is an example of a HTTP response:

HTTP/1.1 200 OK
Date: Sat, 31 Dec 2005 23:59:59 GMT
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 122

<title>Wrox Homepage</title>
<!-- body goes here -->

In the present embodiment, HTTP status code is given status line 200, and the message OK. The status line always contains the state code and the corresponding short messages to avoid confusion. The most common status codes have:
◆ 200 (the OK): find the resources and everything is normal.
◆ 304 (NOT MODIFIED): The resource is not any changes since the last request. This is typically used for caching browsers.
◆ 401 (UNAUTHORIZED): The client does not have access to the resources. This usually makes the browser requires the user to enter a user name and password to log in to the server.
◆ 403 (FORBIDDEN): The client failed to obtain authorization. This is usually after 401 entered an incorrect user name or password.
◆ 404 (NOT FOUND): Resources requested does not exist at the specified location.

After the status line, some headers. Typically, the server returns a header named Data, used to describe the response generated by the date and time (server is usually also returns information about some of its own, although not essential). The next two headers everyone should be familiar with, it is the same as the POST request Content-Type and Content-Length. In this example, the Content-Type header specifies the MIME type HTML (text / html), encoding type is ISO-8859-1 (which is standard for coding American English resources). HTML source file is contained in the body in response to the requested resource (although it may also comprise plain text or other resource types of binary data). The browser will display the data to the user.

Note that this request does not specify the type of the response, but it did not matter to the server. The client knew each type of request will return what type of data, and decide how to use the data.

Guess you like