Fundamentals of HTTP

Source: Internet
Author: User

User access to World Wide Web documents, links between world Wide Web documents, and data transfer from World Wide Web documents to user computers are implemented by Hypertext Transfer Protocol HTTP (Hypertexttransfer Protocol). HTTP is an application-layer protocol that uses TCP connections for reliable transmission, enabling the exchange of various information, such as text, sound, images, and video, on the World Wide Web.

The HTTP protocol is also working in client/server mode, divided into HTTP client and HTTP server two parts, each interaction between the two sides is a request from the client and the server-side response to make up. The procedure provided by the HTTP protocol for users to access the World Wide Web using a browser can be summarized as:

1. The user in the browser type need to access the Web page URL or click on a page link;

2. The browser resolves the IP address of the target webpage by DNS according to the domain name in the URL;

3. The browser establishes a TCP connection with the server on which the Web page resides;

4. The browser sends the HTTP request message, obtains the target webpage the file;

5. The server sends HTTP response messages and sends the target Web file to the browser;

6. Release the TCP connection;

7. The browser renders the content of the Web page, including text, images, sounds, and other displays on the user's computer screen.

The HTTP protocol is not connected, and it uses the services provided by the connection-oriented TCP protocol. At the same time, the HTTP protocol is stateless, that is, the server side does not log client access time and number of times. The initial HTTP protocol version is 1.0, using non-persistent connections (nonpersistentconnection). Each transfer of an object requires 1 round trip time RTT to establish a TCP connection, request and transfer back to the object using 1 round trip time RTT, that is, an average of 2 roundtrip time RTT gets an object. The most widely used is the 1.1 version of the HTTP Protocol (http/1.1), which uses persistent connection (persistentconnection) By default, which means that multiple objects are transmitted using the same TCP connection.

HTTP messages are text-oriented, and each field in the message is a number of ASCII strings, and the length of each field is indeterminate. HTTP has two types of messages: Request messages and response messages. The request message is a message sent from the client to the server, and the response message is a message from the server to the client. The specific format of the request message and the response message are described below.

1. HTTP Request Message Format

The HTTP request message consists of the request line, the request header line, the blank line, and the request data in the following form:

(Request line) method name + Space +url+ Space + version + carriage return line (\ r \ n)

(Request header line 1) keyword + ":" + Space + value + carriage return line (\ r \ n)

......

(Request header row N) keyword + ":" + Space + value + carriage return line (\ r \ nthe)

(blank line) carriage return line (\ r \ n)

(Request data) ...

(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. Finally, a carriage return and a newline represent the end of the request line. For example:

GET www.sdu.edu.cn http/1.1 return line (\ r \ n)

Where the "method" field indicates what the request message wants the server to do, the type of request message is determined by the method used. The main methods of HTTP request message include: GET, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, CONNECT and so on. The most common methods are get and HEAD.

Get is the most common kind of request, when the client to read the document from the server, when you click on a link on the Web page or through the browser's address bar to enter a URL to browse the Web page, use is GET method. The GET method requires the server to place the URL-positioned resource in the data portion of the response message, which is sent back to the client. The GET method is not suitable for transferring private data and large amounts of data.

The head function is similar to GET, except that the server receives a head request and returns only the response header, instead of sending 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.

(2) Request header line (header)

The request header row consists of several lines, each consisting of a keyword and its value, separated by a colon ":", and each line ends with a carriage return line-break representation. The request header notifies the server that there is information about the client request, and the typical request header keywords are:

User-agent: The type of browser that generated the request.

Accept: A list of content types that the client can identify.

Accept-language: Client-recognized language types

Host: The hostname of the request.

Connection: Tells the server to release the connection or remain connected after sending the document.

(3) Blank line

The last request header is followed by a blank line, which sends a carriage return and a newline character, notifying the server that the request header is no longer available.

(4) Request data

The GET method does not have the content of the requested data, and the POST method uses the request data for the client to fill out the form to the server side.

For example, if the browser uses the GET method to access the "School Profile" document (URL www.sdu.edu.cn/2010/xxjj.htm) in the Shandong University homepage, its HTTP request message can be:

get/2010/xxjj.html http/1.1 \ r \ n

host:www.sdu.edu.cn\r\n

user-agent:mozilla/5.0

ACCEPT-LANGUAGE:CN */*\r\n

2. Response message Format

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

(status line) version + Space + status code + Space + phrase + carriage return line

(message header 1) keyword + ":" + Space + value + carriage return line

......

(message header N) keyword + ":" + Space + value + carriage return line

(blank line) carriage return line (\ r \ n)

(Response body) ...

In the status line of the response message, the version Word represents the version of the server HTTP protocol, and the status code word indicates the response status codes sent back by the server, and the phrase field represents the text description of the status code. The status code consists of three-bit decimal digits, the first number defines the category of the response, there are five possible values (1-5), and each status code has the following meanings:

1XX: Indicates the information. Indicates that the request has been received and continues processing.

2XX: Success. Indicates that the request has been successfully received, understood, accepted.

3xx: Redirect. Further action is required to complete the request.

4XX: Client error. The request has a syntax error or the request cannot be implemented.

5XX: Server-side error. The server failed to implement a legitimate request.

Common status codes and status descriptions are described below:

OK: Client request succeeded.

Bad Request: Client requests have syntax errors and cannot be understood by the server.

401 Unauthorized: Request unauthorized.

403 Forbidden: The server receives the request but refuses to provide the service.

404 Not Found: The request resource does not exist, such as entering the wrong URL.

Internal Server error: Unexpected errors occurred on the server.

503 Server Unavailable: The server is currently unable to process client requests and may return to normal after some time.

The message header is similar to the format of the request header and contains several lines, each consisting of a keyword and its value, and commonly used keywords include:

Date: Indicates the time when the message was returned.

Content-type: Represents the content type of the returned message.

Content-length: Returns the length of the content (in bytes).

Server: Use the software and its version number.

Similarly, the last message header is followed by a blank line, which sends a carriage return and a newline character, notifying the client that no longer has a message header.

The response body part is the specific document content that the server side sends back according to the client's request, expressed in HTML language.

Fundamentals of HTTP

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.