preface

HyperText Transfer Protocol (HTTP) is the most widely used network Transfer Protocol on the Internet. Running on TCP, the browser sends a request to the HTTP server through the URL as the HTTP client. After the Web server receives the request, Sends response information to the client.

The characteristics of

Simple and quick

When a client requests service from the server, it only needs to transmit the request method and path to get the response from the server quickly.

There is no connection

Each connection processes only one request. After the server processes the request from the client and receives the response from the client, the server disconnects from the client.

stateless

HTTP protocol is stateless, the server does not remember HTTP state, each time it is a new request (solution see cookie, session…).

URL

Uniform Resource Locator (URL) a Uniform Resource Locator (URL) is an addressing method specially set for identifying the location of resources on the Internet. A Resource Locator is a concise representation of the location of resources that can be obtained on the Internet. It is the address of standard resources on the Internet. < protocol >://< host >:< port >/< path >

The HTTP request

An HTTP request consists of four parts: request line, header, blank line, and request data.

In the browser request to open a web page, capture the packet under the data

GET /test/index.html HTTP/1.1
Host: www.test.cn
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10 _11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.02924.87. Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml; q=0.9,image/webp,*/ *; Q =0.8 Accept-encoding: gzip, deflate, SDCH Accept-language: zh-cn,zh; Q = 0.8, en. Q = 0.6Copy the code

The request line

GET indicates the request type, /test/index.html indicates the resource to be accessed, and HTTP/1.1 indicates the protocol version

The request header

From the second line in the request header, Host indicates the destination of the request (Host domain name); User-agent is client-side information that is important to detect the browser type, defined by the browser, and sent automatically with each request. Accept-encoding Tells the server what type of Encoding the browser supports, usually some kind of compression algorithm

A blank line

The request header must be followed by an empty line

The request data

The requested data, also called the request body, can be added to any other data. The request body for this example is empty.

The HTTP response

The Response message consists of four parts:

The status line

HTTP version number, status code, and status message

The message header

Date: the Date and time when the response is generated. Content-type: specifies the MIME Type of HTML(text/ HTML). The encoding Type is UTF-8. Content-length: specifies the Content Length of the response entity

A blank line

Blank lines are required, as in the Request message

In response to the body

The server returns the HTML code to the client

HTTP request methods

HTTP request methods are used to perform operations on specified resources (HTTP and POST are commonly used, and RESTful interfaces generally use GET, POST, DELETE, and PUT). They are classified into the following types:

1.GET// Request the specified page information and return the entity body.
2.HEAD// This is similar to a GET request, except that there is no concrete content in the response, which is used to GET the header
3.POST// Submit data to a specified resource for processing requests (such as submitting a form or uploading a file). The data is contained in the request body. POST requests may result in the creation of new resources and/or the modification of existing resources.
4.PUT// The data sent from the client to the server replaces the contents of the specified document.
5.DELETE// Request the server to delete the specified page.
6.CONNECT// Reserved in HTTP/1.1 for proxy servers that can pipe connections.
7.OPTIONS// Allow clients to view server performance.
8.TRACE// Displays the requests received by the server for testing or diagnosis.
9.PATCH// Is a complement to the PUT method, which is used to locally update known resources.
Copy the code

The HTTP status code

The HTTP status code is composed of three decimal digits. The first decimal digit defines the type of the status code, and the last two digits do not classify the status code. There are five types of HTTP status codes.

conclusion

HTTP as the most widely used protocol on the Internet, its importance is self-evident, through this we mainly understand HTTP, but also for the next we will talk about TCP to lay a good foundation.