HTTP protocol details

Source: Internet
Author: User

Original article address:

Http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html

 

Today's WebProgramThe development technology of ASP. NET, PHP, JSP, Perl, Ajax, etc.
Regardless of the future development of web technology, it is very important to understand the basic protocols for communication between Web applications because they allow us to understand the internal work of Web applications.
This article will provide detailed examples of the HTTP protocol, and I hope you will be patient. I also hope it will be helpful for everyone's development or testing work. Use the fiddler tool to easily capture HTTP
Request and HTTP response. For more information about how to use the fiddler tool, see my blog [fiddler tutorial].

Reading directory

    1. What is HTTP?
    2. Web server, browser, Proxy Server
    3. URL details
    4. The HTTP protocol is stateless.
    5. HTTP message structure
    6. Difference between get and post Methods
    7. Status Code
    8. HTTP Request Header
    9. HTTP Response Header
    10. The difference between the stateless HTTP protocol and connection: keep-alive

What is HTTP?

Protocol refers to the regulations or rules that must be followed by two computers in a computer communication network. Hypertext Transfer Protocol (HTTP) is a communication protocol, it allows the transfer of Hypertext Markup Language (HTML) documents from the Web server to the client's browser

Currently, HTTP/1.1 is used.

Web server, browser, Proxy Server

When we open the browser, enter the URL in the address bar, and then we can see the webpage. What is the principle?

In fact, after we enter the URL, our browser sends a request to the web server,
After receiving the request, the web server generates the corresponding response and sends it to the browser,
The browser parses the HTML in response, so that we can see the webpage, as shown in the process.

Our request may be sent to the Web server only after it passes through the proxy server.

Shows the process.

The proxy server is the transfer station of network information. What functions does it provide?

1. Improve access speed. Most proxy servers have the cache function.

2. Break through the limitations, that is, turning over the wall.

3. Hide the identity.

URL details

The URL (Uniform Resource Locator) address is used to describe resources on a network. The basic format is as follows:

 
Schema: // host [: Port #]/path /.../[? Query-String] [# anchor]

Scheme specifies the protocol used at the lower layer (for example, HTTP, https, and FTP)

IP address or domain name of the Host HTTP Server

Port # The default port number of the HTTP server is 80. In this case, the port number can be omitted. If another port is used, it must be specified, for example
Http://www.cnblogs.com: 8080/

Path

Data sent from query-string to the HTTP server

Anchor-anchor

URL example

 
Http://www.mywebsite.com/sj/test/test.aspx? Name = sviergn & X = true # stuff

Schema: HTTP
HOST: www.mywebsite.com
Path:/SJ/test. aspx
Query string: Name = sviergn & X = true
Anchor: stuff

CopyCode

The HTTP protocol is stateless.

The HTTP protocol is stateless. The request of the same client does not correspond to the previous request. For the HTTP server, it does not know that the two requests come from the same client. To solve this problem,
The Web Program introduces the cookie mechanism to maintain the status.

HTTP message structure

First, let's look at the structure of the request message. The request message consists of three parts: Request Line and request header,
The third part is that there is an empty line between body. header and body. The structure is as follows:

The method in the first line indicates the request method, for example, "Post", "get", path-to-Resoure indicates the requested resource,
HTTP/version-number indicates the HTTP protocol version number.

When the "get" method is used, the body is empty.

For example, the request for opening the blog homepage is as follows:

 
Get http://www.cnblogs.com/https/1.1
HOST: www.cnblogs.com

Copy code

Abstract things are hard to understand. The old feeling is false. We can understand and remember what we see in reality.
We use fiddler today. Let's take a look at the request and response.

Next we open Fiddler to capture a request for logging on to the blog Park and analyze its structure.
You can view the complete request message in RAW mode, as shown in figure

Let's look at the structure of the response message, which is basically the same as the structure of the request message. It is also divided into three parts, the first part is called response line,
The second part is response header, and the third part is that there is an empty line between body. header and body. The structure is as follows:

HTTP/version-number indicates the HTTP protocol version. For details about status-code and message, see [Status Code.

We use Fiddler to capture the response of a blog homepage and analyze its structure.
You can see the complete response message in raw Mode Under the tab, as shown in

Difference between get and post Methods

The HTTP protocol defines many methods to interact with the server. There are four basic methods: Get, post, put, and delete.
A URL address is used to describe resources on a network. The get, post, put, and delete operations in HTTP correspond to four operations to query, modify, add, and delete resources.
The most common ones are get and post.Get is generally used to obtain/query resource information, while post is used to update resource information.

Let's look at the difference between get and post.

1.
The data submitted by get will be placed after the URL? Splits the URL and transmits data. parameters are connected with each other, for example, editposts. aspx? Name = test1 & id = 123456.
The post method places the submitted data in the body of the http package.

2. The size of the data submitted by get is limited (because the browser has a limit on the URL length), but there is no limit on the data submitted by the POST method.

3. You need to use request. querystring to obtain the value of the variable in get mode, while request. form is used to obtain the value of the variable in post mode.

4.
If you submit data in get mode, security issues may occur. For example, when you submit data in get mode on a login page, the user name and password will appear on the URL, if the page can be cached or accessed by others, you can obtain the user's account and password from the history.

Status Code

The first line in a response message is called a status line, which consists of three parts: HTTP Protocol version number, status code, and status message.

The status code is used to tell the HTTP client whether the HTTP server has produced the expected response.

HTTP/1.1 defines five types of status codes, which are composed of three digits. the first digit defines the category of the response.

1xx prompt message-indicates that the request has been successfully received and continues Processing

2XX success-indicates that the request has been successfully received, understood, and accepted

3xx redirection-further processing is required to complete the request

4xx client error-request syntax error or request cannot be implemented

5xx server-side error-the server fails to implement valid requests

Look at some common status codes

200 OK

The most common is the successful response status code 200, which indicates that the request is successfully completed and the requested resource is sent back to the client.

For example, open the blog Home Page

302 found

Redirection. The new URL will be returned in location in response, and the browser will automatically send a new request using the new URL.

For example, input in IE, The http://www.google.com. HTTP server returns 302, ie gets location in response
The new URL of the header, and then resend a request.

304 not modified

Indicates that the previous document has been cached and can be used again,

For example, when I open the blog homepage, I find that many response status codes are 304.

Tip: if you do not want to use the local cache, press Ctrl + F5 to force the page to be refreshed.

400 bad request client request and syntax errors, which cannot be understood by the server

403 the forbidden server received the request but refused to provide the service.

404 not found

The requested resource does not exist (the URL is incorrect)

For example, enter a wrong URL in IE, http://www.cnblogs.com/tesdf.aspx

500 an unexpected error occurred on the internal server error Server

503 the server unavailable server cannot process client requests currently and may return to normal after a period of time

HTTP Request Header

Use Fiddler to conveniently view the reques header. Click inspectors tab-> request tab->
Shows headers.

There are many headers, which are hard to remember. We also classify headers as fiddler, which is clear and easy to remember.

Cache header domain

If-modified-since

Purpose:
The last modification time of the browser-side cache page is sent to the server. The server compares this time with the last modification time of the actual file on the server. If the time is the same, 304 is returned, and the client directly uses the local cache file. If the time is different, 200 and the new file content are returned. The client discards the old file, caches the new file, and displays it in the browser.

Example: If-modified-since: Thu, 09 Feb 2012 09:07:57 GMT

Example

If-None-match

Purpose: If-None-match and etag work together. The working principle is to add etag information in HTTP response. When the user requests this resource again
Request
If-None-match information (etag value) is added ). If the etag of the server authentication resource is not changed (the resource is not updated), a 304 status will be returned to tell the client to use the local cache file. Otherwise, the system returns the 200 status and the new resource and etag.
Using this mechanism will improve the website's performance

For example, if-None-Match: "03f2b33c0bfc0: 0"

Example

Pragma

Purpose: prevent the page from being cached. In HTTP/1.1, it works exactly as well as cache-control: No-cache.

There is only one usage for pargma, for example: Pragma: No-Cache

Note: In HTTP/1.0, only pragema: No-cache is implemented,
No cache-control is implemented

Cache-control

Role: This is a very important rule. This is used to specify the cache mechanism followed by response-request. The meaning of each instruction is as follows:

Cache-control: public can be cached by any cache ()

Cache-control: Private content is only cached in the private cache.

Cache-control: No-Cache all content will not be cached

There are other usage cases. I do not understand the meaning. Please refer to other documents.

Client header domain

Accept

Role: acceptable media types on the browser,

For example, accept: text/html indicates that the browser can accept text/html, which is also a common HTML document,

If the server cannot return text/HTML data, the server should return a 406 error (non acceptable)

Wildcard * represents any type

For example, accept: */* indicates that the browser can process all types of data. (generally, this is what the browser sends to the server)

Accept-encoding:

Purpose: The browser declares the encoding method it receives. It usually specifies the compression method, whether compression is supported, and what compression method (gzip and deflate) is supported. (Note: This is not only character encoding );

Example: Accept-encoding: gzip, deflate

Accept-Language

Purpose: The browser declares the language it receives.

Differences between a language and a character set: Chinese is a language, and Chinese has multiple character sets, such as big5, gb2312, and GBK;

Example: Accept-language: En-US

User-Agent

Purpose: Tell the HTTP server the name and version of the operating system and browser used by the client.

When we log on to the forum online, we will often see some welcome information, which lists the names and versions of your operating system, the names and versions of your browsers, this often makes many people feel amazing. In fact, the server application obtains the information from the User-Agent Request Header domain, which allows the client to tell the server its operating system, browser, and other attributes.

For example:User-Agent: Mozilla/4.0 (compatible; MSIE
8.0; Windows NT 5.1; Trident/4.0; CBA;. Net CLR 2.0.50727;. Net CLR
3.0.20.6.2152;. Net CLR 3.5.30729;. net4.0c; infopath.2;. net4.0e)

Accept-charset

Purpose: The browser declares the character set it receives. This is the various character sets and character encoding described earlier in this article, such as gb2312 and UTF-8 (we generally say charset includes the corresponding character encoding scheme );

For example:

Cookie/login header domain

COOKIE:

Role: The most important header that sends the cookie value to the HTTP server.

Entity header field

Content-Length

Purpose: The length of the data sent to the HTTP server.

Example: Content-Length: 38

Content-Type

Purpose:

Example: Content-Type: Application/X-WWW-form-urlencoded

Miscellaneous header field

Referer:

Purpose: The server that provides the request context information and tells the server from which the request is sent. For example, if the request is linked to a friend on my homepage, the server can
Referer calculates how many users click the link on my homepage to access his website every day.

Example: Referer: http://translate.google.cn /? Hl = ZH-CN & tab = WT

TRANSPORT header field

Connection

For example, connection: keep-alive
After a webpage is opened, the TCP connection used to transmit HTTP data between the client and the server is not closed. If the client accesses the webpage on the server again, the established connection will continue to be used.

For example, connection: Close indicates that after a request is completed, the TCP connection used to transmit HTTP data between the client and the server is closed,
When the client sends the request again, a TCP connection needs to be established again.

Host (this header field is required when a request is sent)

Purpose: The request header field is used to specify the Internet host and port number of the requested resource. It is usually extracted from the HTTP URL

For example, we enter: http://www.guet.edu.cn/index.html in the browser

The request message sent by the Browser contains the host Request Header domain, as follows:

HOST: http://www.guet.edu.cn

The default port number 80 is used here. If the port number is specified, it is changed to: Host: Specifies the port number.

HTTP
Response Header

Similarly, use Fiddler to view the response header and click inspectors tab-> Response tab->
Shows headers.

We also classify headers as fiddler, which is clear and easy to remember.

Cache header domain

Date

Purpose: specify the time and date when the message is generated.

Example: Date: sat, 11 Feb 2012 11:35:14 GMT

Expires

Purpose: The browser uses the local cache within the specified expiration time.

Example: expires: Tue, 08 Feb 2022 11:35:14 GMT

Vary

Purpose:

Example: vary: Accept-Encoding

Cookie/login header domain

P3p

Purpose: set the cookie for cross-origin access. This can solve the problem of cross-origin access cookie for IFRAME.

Example: p3p: Cp = Cura ADMA Deva psao psdo our bus uni pur int DEM sta pre com nav
OTC Noi DSP Cor

Set-Cookie

Function: a very important header used to send a cookie to the client browser. Each cookie written generates a set-Cookie.

Example: set-COOKIE: SC = 4c31523a; Path =/; domain = .acookie.taobao.com

Entity header field

Etag

Purpose: use it with if-None-match. (See the IF-None-match instance in this section)

Example: etag: "03f2b33c0bfcc0: 0"

Last-modified:

Purpose: indicates the last modification date and time of the resource. (See the IF-modified-since instance in the example)

Example: Last-modified: Wed, 21 Dec 2011 09:09:10 GMT

Content-Type

Purpose: The Web server informs the browser of the type and character set of the object to respond,

For example:

Content-Type: text/html; charset = UTF-8

Content-Type: text/html; charset = gb2312

Content-Type: image/JPEG

Content-Length

Specifies the length of the Object Body, expressed in decimal digits stored in bytes. In the process of data downlink, the Content-Length Method needs to cache all data on the server in advance, and then all the data is sent to the client.

Example: Content-Length: 19847

Content-Encoding

The Web server shows the compression method (gzip, deflate) used to compress the objects in the response.

Example: Content-encoding: Gzip

Content-language

Purpose: The Web server tells the browser the language of the response object.

For example, content-language: da

Miscellaneous header field

Server:

Purpose: Specify the software information of the HTTP server.

For example, server: Microsoft-IIS/7.5

X-ASPnet-version:

Purpose: If the website is developed using ASP. NET, this header is used to indicate the version of ASP. NET.

Example: X-ASPnet-version: 4.0.30319

X-powered-:

Purpose: indicates the technology used for website development.

Example: X-powered-by: ASP. NET

TRANSPORT header field

Connection

For example, connection: keep-alive
After a webpage is opened, the TCP connection used to transmit HTTP data between the client and the server is not closed. If the client accesses the webpage on the server again, the established connection will continue to be used.

For example, connection: Close indicates that after a request is completed, the TCP connection used to transmit HTTP data between the client and the server is closed,
When the client sends the request again, a TCP connection needs to be established again.

Location header field

Location

Role: Used to redirect a new location, including a new URL address

For more information about instances, see 304 status instances.

The difference between the stateless HTTP protocol and connection: keep-alive

Stateless means that the Protocol has no memory for transaction processing, and the server does not know the client status. On the other hand, there is no connection between opening a webpage on a server and the webpage on the server you opened before.

HTTP isStatelessOfConnection-orientedThe stateless protocol does not mean that HTTP cannot maintain a TCP connection, nor does it mean that HTTP uses a UDP Protocol (No connection)

Starting from HTTP/1.1, keep-alive is enabled by default to maintain the connection feature. To put it simply, after a webpage is opened, the TCP connection between the client and the server for transmitting HTTP data will not be closed. If the client accesses the webpage on the server again, it will continue to use this established connection.

Keep-alive does not keep the connection permanently. It has a retention time, which can be set in different server software (such as APACHE ).

If you think this blog has some benefits for you, click [recommendation] in the lower right corner.

If you want to repost this blog, please indicate the source

If you have any comments or suggestions for this article, please leave a message.

Thank you for reading this article. Please follow up on my blog

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.