[Summary-HTTP Protocol]

Source: Internet
Author: User
1. Introduction to HTTP

What is HTTP? Hypertext Transfer Protocal, a hypertext transfer protocol, has been widely used on WWW since 1990. It is the most widely used protocol on WWW today. Its current version is 1.1.

HTTP is an application layer protocol. When you browse the Web page online, the browser and the Web server send and receive data over HTTP over the Internet.

HTTP is a stateless protocol based on the request/response mode. This is what we usually call request/response.

Ii. HTTP URL

The http url format is as follows:

Http: // host [":" port] [abs_path]

HTTP indicates that you want to use HTTP to locate network resources. Host indicates a valid Internet host domain name or IP address. Port is used to specify a port number. The requested resource server host listens to the TCP connection of this port. If the port is empty or is not provided, the default value 80 is used. abs_path indicates the URI of the requested resource (Uniform Resource Identifier, unified resource identifier). If abs_path is not provided in the URL, when it is used as the request URI, it must be given in the form. Usually, this job is done by the browser. When we enter www.cnblogs.com in the browser, and then press enter, the browser will automatically convert the address you entered into http://www.cnblogs.com/, and then the last line is shown. Try it.

Iii. http request

The client sends an HTTP request to the server for resource access. Http consists of three parts: request line, message header, and request body.

Request Line:

The request line starts with a method symbol, followed by the request URI and Protocol version, ending with CRLF. Request lines are separated by spaces. separate CR or lf characters are not allowed except for CRLF. The specific format is as follows:

Method Request-Uri http-version CRLF

Method indicates the request method. Request-Uri is a uniform resource identifier, indicating the resource to be requested. http-version indicates the HTTP protocol version of the request, and CRLF indicates the carriage return line feed. For example:

GET/form.html HTTP/1.1 (CRLF)

The get method is used to obtain the resource information represented by request-Uri. The common form is:

GET request-Uri HTTP/1.1

When we access a webpage by directly entering the URL in the address bar of the browser, the browser uses the get method to obtain resource information from the server.

The post method is used to send a request to the target server, requiring the server to receive the data attached to the request. The post method is usually used for form submission.

An example submitted using the POST method:

Post/form.html HTTP/1.1 (CRLF)

Accept: image/GIF, application/JSON (... Omitted) (CRLF)

HOST: www.abc.com (CRLF)

Content-Length: 22 (CRLF)

Connection: keep-alive (CRLF)

Cache-control: No-Cache (CRLF)

(CRLF) // note: the upper part of the empty line is the message header, and the lower part is the request body. This empty line is required.

User = zhangsan & Password = 123456

The head method is almost the same as the get method. The difference is that the head method only requests the message header, not the complete content. For the response part of a head request, the information contained in the HTTP header is the same as the information obtained through the GET request. With this method, you do not need to transmit the content of the entire resource to obtain the resource information represented by request-Uri. This method is often used to test the validity, accessibility, and recent updates of hyperlinks.

Iv. Http response

After receiving and processing a message, the server returns an HTTP Response Message. Similar to HTTP requests, HTTP responses are composed of three parts: Status line, message header, and response body.

Status line:

A status line consists of a Protocol version, a status code in numeric form, and a status description of the response. Each element is separated by a space, except for the ending CRLF (carriage return line feed, cr or lf characters are not allowed. The format is as follows:

HTTP-version status-code reason-phrase CRLF

HTTP-version indicates the HTTP protocol version of the server, and status-code indicates the response code sent back by the server. For details, refer to this Article. [Do you know what the various statuses of hhtp mean ?]. Reason-phrase indicates the text description of the status code, and CRLF indicates the carriage return and line feed. For example:

HTTP/1.1 200 OK (CRLF)

V. Request Header Format

 

 

1. General Header

Cache-control: the client wants the server to cache its request data, for example, "cache-control: No-Cache", "cache-control: Max-age = 0 ";
Connection: whether the client wants to maintain a persistent connection with the server, for example, "connection: Close", "connection: keep-alive ";
Date: the client may have some fields only when the request method is post or put;
Pragma: contains some special request information of the client, such as "Pragma: No-Cache". The client wants the proxy or application server to not cache result data related to the request.

2. Request Header

Accept: Specifies the list of media types that the client can accept in response to a request. The asterisk (*) is used to group types by range. The asterisk (*)/* indicates that all types are acceptable. The asterisk (*) indicates that all types of subtypes are acceptable, for example, "accept: image/GIF, image/JPEG ,*/*";
Accept-charset: the encoding format of the character set that the client can recognize. Format: "Accept-charset: Character Set 1 [: weight], Character Set 2 [: weight]", for example: "accept-charset: iso-8859-5, unicode-1-1; q = 0.8 ";
Accept-language: the language that the client can recognize. Format: "Accept-language: Language 1 [: weight], language 2 [: weight]", for example: "Accept-language: en, en; q = 0.7 ";
HOST: the host domain name or Host IP Address requested by the customer. Format: "Host: domain name or IP [: port number]", for example, "Host: www.cnblogs.com: 80", this request header is required if the request line contains HTTP/1.1;
User-Agent: indicates the browser ID used by the user. It is mainly used for statistical purposes;
Referer: indicates the connection from which the request is sent;
Accept-encoding: the encoding compression format that the client can recognize, for example, "Accept-encoding: gzip, deflate ";
If-modified-since: this field is related to the Client Cache. Has the URL accessed by the client been modified on the server since the specified date, if the request is modified, the server returns the new modified information. If the request is not modified, the server returns 304, indicating that the request URL has not been modified, for example, "If-modified-since: Fri, 2 Sep 2011 19:37:36 GMT ";
If-None-Match: this field is related to the Client Cache. The client sends this field and its ID when sending a URL request. If the server ID is consistent with the client ID, if 304 is returned, the URL is not modified. If the URL is inconsistent, the server returns the complete data, for example, "If-None-Match: 0f0a893aad8c61: 253, 0f0a893aad8c61: 252, 0f0a893aad8c61: 251 ";
COOKIE: it is an extended field stored on the client and sends a cookie to the server of the same domain name, for example, "Cookie: mailusername = whouse ";

3. Object Header

Content-encoding: the encoding and compression formats that the client can recognize, for example, "content-encoding: gzip, deflate ";
Content-Length: The content length of the Data body when the client uploads data using the POST method, for example, "Content-Length: 24 ";
Content-Type: The content type of the Data body sent by the client. For example, "Content-Type: Application/X-WWW-form-urlencoded" indicates the data sent using the normal post method; "Content-Type: multipart/form-data;

6. Response Header Format

1. General Header

Cache-control: the server requires the intermediate proxy and the client to cache their response data, for example, "cache-control: No-Cache", for example, "cache-control: private "does not want to be cached." cache-control: Public "can be cached;
Connection: whether the server wants to maintain a persistent connection with the client, such as "connection: Close" and "connection: keep-alive ";
Date: the client may have some fields only when the request method is post or put;
Pragma: contains some special response information of the server, such as "Pragma: No-Cache". The server wants the proxy or client to not cache result data;
Transfer-encoding: the transmission mode used by the server to transmit data to the client (only available in http1.1), for example, "transfer-encoding: chunked". Note: the priority of this field is higher than that of the Content-Length field;

2. Request Header

Accept-ranges: indicates the Data Unit received by the server, for example, "Accept-ranges: bytes ",;
Location: the server returns this information to the client for redirection, for example, "Location: http://www.abc.com ";
Server: Some information returned by the server to identify itself, such as: "server: Microsoft-IIS/6.0 ";
Etag: the Identifier Field of the response data returned by the server. The client can send the updated URL information to the server based on the value of this field.

3. Object Header

Content-encoding: encoding format of the data returned by the server, for example, "content-encoding: gzip ";
Content-Length: The content length of the Data body returned by the server, for example, "Content-Length: 24 ";
Content-Type: content type of the Data body returned by the server, for example, "Content-Type: text/html; charset = gb2312 ";
Set-COOKIE: the cookie data that the server returns to the client, for example, "set-COOKIE: Asp. net_sessionid = icnh2ku2dqlmkciyobgvzl55; Path = /"

 

VII. Use of Telnet Tool

1. Use the Telnet tool provided by Microsoft to connect to the specified port of the remote host (you must manually add this service in Windows Server 2008 before using it ).
2. Telnet can be used to connect to the host with the specified port. For example:
CMD --> Telnet www.cnblogs.com 80 to connect to the host of cnblgos
Then enter the head request:
Headers/index.html HTTP/1.1
HOST: www.cnblogs.com
The echo function may not be displayed. You can use the set localecho command to set the echo function. Finally, the response is output.

 

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.