HTTP request principle

Source: Internet
Author: User

The client sends a HTTP request-to-server request messages include the following format: Request line, request header (header), blank line, and request data four parts, giving the general format of the request message.

Request Line

The HTTP response is also made up of four parts: the status line, the message header, the blank line, and the response body.

Here are some of the most common request headers:

Accept: The MIME type acceptable to the browser.
Accept-charset: The acceptable character set of the browser.
Accept-encoding: The way the browser can decode data encoding, such as gzip. The servlet can return a GZIP-encoded HTML page to a browser that supports gzip. In many cases this can reduce download time by 5 to 10 times times.
Accept-language: The type of language the browser wishes to use when the server is able to provide more than one language version.
Authorization: Authorization information, which typically occurs in an answer to the Www-authenticate header sent to the server.
Connection: Indicates whether a persistent connection is required. If the servlet sees the value here as "keep-alive", or sees the request using an HTTP 1.1 (HTTP 1.1 is persistent by default), it can take advantage of the persistent connection, when the page contains multiple elements (such as applets, pictures), Significantly reduce 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.
Content-length: Represents the length of the request message body.
Cookies: This is one of the most important request header information, as discussed in the following chapter, "Cookie Processing".
From: The email address of the requesting sender, used by some special Web client, is not used by the browser.
Host: The hosts and ports in the initial URL.
If-modified-since: Returns a 304 "not Modified" answer only if the requested content has been modified after the specified date.
Pragma: Specifying a value of "no-cache" means that the server must return a refreshed document, even if it is a proxy server and has a local copy of the page.
Referer: Contains a URL from which the user accesses the currently requested page from the page represented by the URL.
User-agent: Browser type, this value is useful if the content returned by the servlet is related to the browser type.
UA-PIXELS,UA-COLOR,UA-OS,UA-CPU: A nonstandard request header sent by some versions of Internet Explorer to indicate screen size, color depth, operating system, and CPU type.

HTTP status Code

When a viewer accesses a webpage, the browser of the viewer makes a request to the server where the page is located. When a Web page is received and displayed by the browser, the server on which the page resides returns a message header (server header) that contains the HTTP status code to respond to the browser's request.

HTTP status code in English is HTTP status code.

the following are common HTTP status code:

200-Successful request

301-Resources (Web pages, etc.) are permanently transferred to other URLs

404-The requested resource (Web page, etc.) does not exist

500-Internal Server error

· HTTP request Flow

      First, HTTP belongs to the application-layer protocol in the TCP/IP model. and two applications (we refer to the browser and the server) to communicate with each other, the first to establish a TCP connection, and then the browser can send the request information to the server, the server after receiving the request information, return the corresponding response information, the browser received from the server response information, Interpret the data for execution.

      in the version of HTTP 1.0, each request of the browser (that is, access to each page) requires a separate connection, and automatically releases the connection after each request is processed. (We should all have feelings, such as we visit a page, when the page is displayed in the browser, we can unplug the network cable, the information on this page is not lost.) And when we request the webpage file has many pictures, music, movie and so on information, the server returns the information does not contain the picture data directly, but only saves the picture the link, when the browser interprets, encounters the picture the URL, only then sends the request information to the picture to the server. It can be seen that if a Web page contains multiple picture data, it will be frequently connected with the server, and release the connection, which will undoubtedly lead to waste of resources.

HTTP 1.0 Request Mode

HTTP 1.1 can handle multiple requests in a single connection, and multiple requests can overlap, without waiting for a request to end before sending the next request.

· HTTP Request message

1 Complete HTTP request messages include a request line, a number of message headers, and the entity content, while the message header and entity content may not have a blank line between the message header and the entity content.
Let's take a look at an example (for illustrative purposes, I add a serial number before each line):
1 get/mattmarg/http/1.0
2 user-agent:mozilla/2.0 (Macintosh; I; PPC)
3 accept:text/html; */*
4 Cookie:name = value
5 referer:http://www.xxx.com/a.html
Where line 1th is the request line: The request is get (in addition to get, post, Put, delete), the requested file is located under "root directory/mattmarg/", of course, you can also directly give the desired page (such as:/mattmarg/ Index.asp, can also add some other fields such as:/mattmarg/index.asp? Id=1&uid=xxx. When we pass a GET request, the request line that is submitted to the server cannot exceed 1K, and if the post is used, the submitted information is sent to the server in the form of entity content, so if the server has no limit, it can transmit unlimited content in principle, http/1.0 Indicates that the version of HTTP is 1.0. The other lines are the message headers, which are primarily used to communicate some kind of message or instruction to the server. such as telling the server its own terminal (user-agent) is what (if the browser is returned to the corresponding browser model), the terminal can explain what type (Accept) is the request from which page submitted (Referer), And the language that the browser can interpret (accept-language) and so on. We take accept-language here to give an example, we all know that Google in mainland China display is Simplified Chinese, and in other countries show the corresponding language, how is this done? In fact, the browser submits the request message to the server contains the Accept-language, and our browser is ZH-CN by default, and the server returns the corresponding page when it receives the information.
We can verify this in the following ways:
1. Open browser, Tools->internet options, General tab 2, select "Language", the default language is Chinese 3, select "Add", select a language, and then adjust the order of precedence
4, after confirming, we again visit http://www.google.com/, is not found that the original Simplified Chinese all became the traditional characters.

· HTTP Response message

The format of an HTTP response message is a status line, a number of message headers, and entity content, where the message header and entity content can be not, and there is a blank line between the message header and the entity content.
We will still look at an example first:
http/1.1-OK
server:microsoft-iis/5.1
X-powered-by:asp.net
Date:sun, Jul 11:01:21 GMT
Content-type:text/html
Accept-ranges:bytes
last-modified:wed, 01:01:26 GMT
ETag: "0f71527dfdbc81:ade"
content-length:46
10
where 01 lines is the status line to show the status of the server response, http/1.1 shows the corresponding version of the HTTP protocol, 200 is the status number, OK for state information to interpret the status number (here OK corresponds to 200, indicating that the request is normal); 02~09 is the message header, 10 is a blank line, 11 is the entity content (that is, the Web page content returned by the server).

Well, I believe you should have a general understanding of the process of HTTP requests, then we will answer the original question: when we enter "http://www.baidu.com/" in the Address bar of the browser "and then press" enter ", what happened after that? .

First, the browser locates the IP that the URL points to, then establishes a TCP connection with it, then puts forward a GET request to the Baidu server, when the server receives our request, sends us the reply message-Baidu's page, then disconnects.
Reference: Http://www.cnblogs.com/stg609/archive/2008/07/06/1236966.html

HTTP request principle

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.