HTTP Super Detailed Tutorial

insert image description hereinsert image description here


1. HTTP protocol

1.1, a brief description of HTTP

The full name of HTTP is Hypertext Transfer Protocol, which is a widely used application layer protocol.

So what is hypertext? Hypertext refers to the transmission of content not only text, such as html, css, javaScript and other data, but also some other resources, such as pictures, videos, audio and other binary data.

The schematic diagram of OSI reference model and TCP/IP layered model is as follows:

1.2, HTTP development history

HTTP was born in 1991 and has developed into the most mainstream application layer protocol.

Schematic diagram of the development process of HTTP:

image-20230520162711248

tips:

HTTP is often implemented based on the TCP protocol of the transport layer, but HTTP is also implemented based on the UDP protocol of the transport layer.

  • HTTP1.0, HTTP1.1, and HTTP2.0 are all based on TCP, while HTTP3 is based on UDP.

Although HTTP has now developed to the HTTP3 version, currently we mainly use HTTP/1.1 and HTTP/2.0 versions.

1.3, HTTP working process

The HTTP protocol is a hypertext transfer protocol, an application layer communication protocol between a client or other programs and a web server.

In other words, the HTTP protocol is the rule for data transmission between the server and the client.

What is the working process of HTTP?

image-20230520170122009

**The working process of HTTP is as follows: **When we enter a "web address" (URL) searched by Sogou in the browser, the browser sends an HTTP request to Sogou's server. After the HTTP request from the server, an HTTP response is returned to the browser, and the HTTP response result returned from the server is parsed by the browser, and then displayed as the content of the page we see.

During this process, the browser may send multiple HTTP requests to the server, and the server will return multiple responses correspondingly. These responses include a series of content such as HTML, CSS, JavaScript, pictures, and fonts of the page.

That is to say, when we visit a website, it may involve more than one HTTP request/response interaction process.

Expansion: Application layer protocol understanding

The transport layer is located in the fourth layer of the OSI model and is responsible for providing end-to-end data transmission services between different hosts in the network, including functions such as network connection, data flow segmentation, data transmission error control, data flow reassembly, and flow control. Path selection and cross-network transmission are part of the function of the transport layer, which are completed through protocols such as IP (International Protocol) and TCP (Transmission Control Protocol). When data is transmitted across the network through path selection, the transport layer needs to encapsulate and decapsulate the data, including operations such as segmentation, assembly, and connection control of the data to ensure the integrity and correctness of the data. Therefore, the transport layer is the key layer to realize the data transmission between the source port and the destination port.

Through the above understanding of the transport layer, we already know that in the transport layer, data can be transmitted across the network from the source port process to the destination port process through path selection.

However, is the process over just by transferring data from the source port to the destination port or from the destination port to the source port?

This is obviously not over, whether the data is transmitted from the server to the client, or after the data is transmitted from the client to the server, the server and the client still need to process or use the data. At this time, we need a layer of protocol, which does not care about communication details, but only cares about application details, and this layer of protocol is called application layer protocol.

Remarks: Client and server are concepts of application layer, while source port process and destination port process are concepts of transport layer.

Because there are many application scenarios, there are also many types of application layer protocols, among which the HTTP protocol is a relatively classic application layer protocol.

A simple understanding of application layer protocols through life examples:

When we purchase an electronic product on Taobao, the seller [client] will send the product to the buyer [server] through SF Express [delivery + route selection], and the product in the hands of the buyer will be accompanied by a product Manual, the manual contains information such as product introduction, usage introduction and precautions, so as to guide users how to use the product. At this time, the manual is equivalent to the application layer protocol.


2. HTTP protocol format

HTTP is a protocol in text format, which can be viewed through Chrome developer tools or third-party tools to capture packets, and then analyze the details of HTTP requests and HTTP responses.

2.1, use developer tools to obtain

1. Use of developer tools

There are two ways to open the developer tools, namely:

1) In the browser page, click the right mouse button and select the check option to open the developer tool

image-20230520193540042

2) Directly use the shortcut key F12to open the developer tool

Open the developer tools of chrome, switch to the Network (network) tab, and then refresh the page to see the following effect:

image-20230520194151721

2. The results of the developer tools

When you want to view the specific information of a record, click the record name.

image-20230520195843574

2.2, use the packet capture tool to obtain

1. Use of packet capture tools

image-20230520200712105

1. The left window displays all HTTP requests/responses, and you can select a request to view details

2. The message content of the HTTP request is displayed on the upper right (switch to the Raw tab to see the detailed data format)

3. The message content of the HTTP response is displayed on the lower right (switch to the Raw tab to see the detailed data format)

4. The detailed data of the request and response can View in Notepadbe opened by Notepad in the lower right corner

5. You can use ctrl + aall the capture results on the left to select all, and press the delete key to clear all the selected results

2. The principle of packet capture tool

The packet capture tool Fiddler is essentially equivalent to a "proxy".

**The working principle is: **When the browser visits www.sogou.com, it will first send the HTTP request to Fiddler, and then Fiddler will forward the request to the server of sogou. When the sogou server returns the data, Fiddler gets the returned data, and then passes the data to the browser. Therefore, Fiddler is very clear about the data details of the interaction between the browser and the sogou server.

Schematic diagram of packet capture tool Fiddler:

image-20230520202152673

There are two precautions when using the packet capture tool Fiddler:

1. There may be conflicts with other proxy programs. When using it, other proxy programs and some browser plug-ins should be closed.

2. In order to capture packets correctly, you need to enable the HTTPS function. Fiddler cannot capture HTTPS packets by default, and most of the servers on the Internet currently use HTTPS. HTTPS introduces an encryption layer based on HTTP, so we will You need to manually enable the HTTPS function and install the certificate.

Expansion: Fiddler opens HTTPS function

image-20230520204154287

Expansion: forward proxy and reverse proxy

**Forward proxy (forward proxy)** refers to a server (proxy server) between the client and the target server. In order to obtain content from the target server, the client sends a request to the proxy server and specifies the target, and then The proxy server forwards the request to the target server and returns the obtained content to the client.

Life example: You and your good buddy are hanging out at school, and you see a very beautiful young lady. You want to know the WeChat of this young lady, but you dare not ask because you are introverted. Hello buddy, in order to help you, I ran over to help you to ask this little sister's WeChat, and I will give you the little sister's WeChat when I come back.

Example explanation: You are equivalent to the client, and your good buddy is equivalent to the proxy server, and that young lady is equivalent to the server.

The little sister only knows that your good buddy helped her in the past, but she doesn't know who wants to add her WeChat, and you clearly know whose WeChat you want.

This shows that the client can know the address of the target server, but the target server does not know which client it is from, he only knows which proxy server it is from.

Therefore, the forward proxy can shield or hide the client's information.

**Reverse proxy (reverse proxy)** refers to the proxy server to receive the client's request, then forward the request to the server on the internal network, and return the result obtained from the server to the client. At this time, the proxy server externally Acts as a reverse proxy server.

Life example: You invite friends to dinner in a hotel, the waiter will give the menu you ordered to the back kitchen, and the head chef in the back kitchen will distribute the dishes in the menu to the chefs below to make, and after the preparation, the waiter will serve it Serve at the table.

Example: Your table is equivalent to the client, the waiter is equivalent to the proxy server, and the chef in the back kitchen is equivalent to the server.

You only know that the dish was served by the waiter, but you don't know which chef made the dish, but the chef in the back kitchen knows which table the dish is for.

This shows that the server can know the address of the client, but the client does not know which server it is from, he only knows which proxy server it is from.

Therefore, the reverse proxy can shield or hide the information of the server.

3. The result of the packet capture tool

1. HTTP list

image-20230520205729119

2. HTTP request

image-20230520205301852

3. HTTP response

image-20230520205337498

4. Analysis of packet capture results

The above HTTP list is the HTTP request generated when the browser accesses the Sogou homepage.

Among these requests, we are most concerned about what we request to access, and the rest of the requests are generated based on this request.

When searching for a page in the browser, there may be one or multiple corresponding HTTP requests.

HTTP requests and HTTP responses have a certain format, and Fiddler will parse them according to different formats and present different display effects.

tcpThe HTTP request and HTTP response obtained by packet capture are data in a line text format. Compared with this binary format, the line text format has the advantage that it is convenient for users to directly view information.

In some servers, the HTTP response data will be compressed by the server to convert the response data into a binary format, thereby saving bandwidth and improving transmission efficiency.

Expansion: compression

Compression is defined as converting raw data into a more compact form in order to save space and bandwidth during storage and transmission. Compression is a common technique used in computer, communication and multimedia applications in general.

Note that not all data is suitable for compression, and some data may be larger after re-encoding, but general HTMLand JSsuch text files are suitable for compression.

2.3, HTTP protocol format

1. HTTP request format

image-20230520220127184

2. HTTP response format

image-20230520215433280

3. Summary of HTTP format

Expansion: Why is there a "blank line" in the HTTP message?

Because the HTTP protocol does not specify the number of key-value pairs in the header part, a blank line is equivalent to "the end marker of the header" or "the delimiter between the header and the body".

HTTP relies on the TCP protocol at the transport layer, and TCP is byte-oriented. If there is no such blank line, there will be "sticky package problem".


3. HTTP request

3.1, HTTP method

1. HTTP request method

HTTP method refers to HTTP defines a set of request methods to indicate the operation to be performed on a given resource or to indicate the desired action to be performed on a given resource.

The common HTTP methods are as follows:

HTTP method Method Description supported version
GET The resource identified by the URL used to fetch the request line 1.0,1.1
POST Used to submit entities to the specified resource 1.0,1.1
PUT Used to transfer specified files 1.0,1.1
HEAD The response header used to get the resource identified by the URL 1.0,1.1
DELETE Used to delete the resource identified by the URL 1.0,1.1
TRACE Used to perform message loopback testing along the path to the target resource 1.1
OPTIONS Allowed communication options for requesting a given URL or server 1.1
CONNECT Used to establish a tunnel to the server identified by the target resource, the connection proxy 1.1
LIKE Used to establish links with resources 1.0
UNLICK Used to disconnect from resources 1.0

If you want to have a deeper understanding of the above HTTP methods, you can learn in [Development Documentation]( HTTP Request Methods - HTTP | MDN (mozilla.org) ).

In actual development, most of the above methods are not used, and the most common HTTP methods are GET方法and POST方法.

2. GET request method

GET is the most commonly used HTTP method, often used to obtain a resource on the server.

Enter the URL directly in the browser, and the browser will send a GET request at this time; in addition, the link, img, script and other tags in the HTML will also trigger the GET request.

In addition to the above two methods of triggering GET requests, Ajax in JavaScript can also be used to construct GET requests.

Visit the Sogou homepage and use the Fiddler packet capture tool to view the packet capture results

As can be seen from the above packet capture results, the top packet capture record is a GET request sent through the browser address bar.

Some of the following requests related to the sogou domain name are generated through the link/scripy/img tags in html, such as the following requests:

There are also some requests generated through Ajax, such as the following requests:

Select the first packet capture record to observe the detailed results of the request.

image-20230521232341877

Features of GET requests:

1) The HTTP method of the request line is GET, and the URL query stringcan be empty or not.

2) The header part of the request header has several sets of attributes of the key-value pair structure. Each set of key-value pairs occupies a single line, and the header part ends with a blank line.

3) The body part of the request body is empty.

3. POST request method

The POST method is also a common HTTP method, often used to submit data entered by the user to the server (such as a login page).

A POST request can be constructed through a form tag in HTML, or a POST request can be constructed using JavaScript ajax.

Log in to the school's official website and use the Fiddler packet capture tool to view the packet capture results

image-20230521231721941

As can be seen from the above packet capture results, the top packet capture record image-20230522212921983is a POST request constructed through a form form.

Select the first packet capture record image-20230522212921983to observe the detailed results of the request.

image-20230521232208104

Features of POST requests:

1) The HTTP method of the request line is POST, and the URL query stringis generally empty, or it may not be empty.

2) The header part of the request header has several sets of attributes of the key-value pair structure. Each set of key-value pairs occupies a single line, and the header part ends with a blank line.

3) The body part of the request text is generally not empty, the data format in the body is specified in the header Content-Type, and the length of the body is specified in the header Content-Length.

4. The difference between POST and GET

Typical differences between POST and GET:

1) GET requests are generally used to obtain data from the server, while POST requests are generally used to submit data to the server.

2) The body of a GET request is generally empty, and the data to be transferred is passed through query string, while the body of a POST request query stringis generally empty, and the data to be transferred is passed through the body.

3) GET requests are generally idempotent, while POST requests are generally not idempotent.

4) The results of GET requests can be cached, but the results of POST requests cannot be cached. (This is to undertake idempotence)

Note: There is actually no essential difference between GET requests and POST requests. The above differences are only differences in usage habits. In most scenarios, the two can be used interchangeably.

Expansion: The core feature of idempotence is that the impact of any number of executions is the same as that of one execution.

5. Extension of GET request

1. GET request transfer data volume

The size of the GET request transmission data is not clearly stipulated in the rfc standard document, that is to say, the HTTP protocol does not stipulate the maximum capacity of the GET request transmission data, which can be infinite in theory, but because the GET request uses the parameter information as a query string The form of the URL is placed after the URL, and many browsers generally limit the length of the URL to no more than 2kb. The limitation of the URL length limits the capacity of the GET request to transmit data during the actual transmission of data.

And there may be a question when you see this, is there a limit to the length of the URL in the GET request?

The HTTP protocol is defined by the RFC 2616 standard. The original text of the standard clearly states: "Hypertext Transfer Protocol -- HTTP/1.1," does not specify any requirement for URL length, that is, there is no limit to the length of the URL in the HTTP protocol, and the actual length of the URL depends on the implementation of the browser and the implementation of the HTTP server. On the browser side, different browsers have different maximum lengths; on the server side, this length can be configured by yourself without limitation.

When the URL length of a GET request exceeds the limit of the browser or server, the request will be truncated or rejected. Therefore, if a large amount of data needs to be passed, it is recommended to use a POST request or store the data on the server side and refer to it by an identifier.

In short, there is no limit to the size of data transmitted by GET request, but due to the limitation of the server (browser), this limitation refers to the limitation of the query string in the url, which in turn leads to the transmission of data capacity in GET request mode. restricted.

2. GET request security

On the surface, because the parameter information of the get request method will be displayed in plain text on the URL address bar, while the parameters passed by the post request method are hidden in the text content, generally non-professional users will not open the developer mode to view, so post and get Compared with the request method, post is relatively more secure; in the deep level, whether it is safe or not depends on whether the front-end performs encryption operations when transmitting sensitive information such as passwords and whether the encryption method is good or bad, and the request method is GET or Is POST irrelevant.

3. GET request transfer data type

GET requests can transmit not only text data, but also binary data, such as pictures, audio, video, etc. However, since GET requests transfer data in clear text in the URL, it is not suitable for transferring large binary data. Because the URL may contain some special characters, such as spaces, +, /, etc., which will affect the integrity and correctness of the data.

GET requests are not suitable for transferring large amounts of binary data, so what about transferring a small amount of binary data?

If only a small amount of binary data needs to be transferred, a GET request can be used. In this case, the binary data can be converted to URL-encoded format by url encode, and then added to the query string of the URL.

3.2, HTTP URLs

URL is Uniform Resource Locatorthe abbreviation of URL, called Uniform Resource Locator, and this is what we usually call the URL.

Every file on the Internet has a unique URL, which contains content information indicating the path of the file and how the browser should behave (that is, how it should be handled).

The detailed rules of URL are stipulated by Internet standard RFC1738. (https://datatracker.ietf.org/doc/html/rfc1738)

Schematic diagram of the URL format:

1) https: Protocol scheme name, the common ones are http and https, and there are other types, such as those used when accessing mysql jdbc:mysql.

2) user:pass: Login information, the identity authentication of the current website is generally no longer carried out through the URL, and is generally omitted.

3) www.example.jp: Server address, here is a "domain name", the domain name will be resolved into a specific IP address through the DNS system, and the real IP can be seen through ping命令.

4) 80: Port number, used to describe which program is used, which can be omitted. When the port number is omitted, the browser will automatically determine which port to use based on the protocol type.

  • For example, http protocol uses port 80 by default, and https protocol uses port 443 by default.

5) /dir/index.html : File path with hierarchy, used to find which file under the program directory, can be omitted.

6) ?userId=1 : query string (query string), essentially a key-value pair structure, key-value pairs are separated by &, and keys and values ​​are separated by =.

7) #ch1: Fragment identifier, which is mainly used to realize in-page jump and can be omitted.

A specific URL:

image-20230523104758365

From the above URL, we can see that some information in the URL is omitted, such as login information, server port number, and fragment identifier.

1. Server address

The server address is also called a domain name, and this domain name will be resolved into a specific IP address through the DNS system, and the real IP can be seen through ping命令.

Expansion: Use the ping command to view the IP address corresponding to the domain name

1) Enter cmd in the start menu to open the command prompt.

2) Enter the domain name in the command prompt, and you can see the result of domain name resolution.

2. Hierarchical file paths

/Represents the root directory, that is, the root directory of the HTTP server, which can be any directory on the system.

The HTTP server is a process on the system, and the server is entrusted to manage a specific directory on the system, and any resource on this directory can be accessed from the outside.

Generally speaking, there are many resources provided in an HTTP server, and different corresponding resources can be obtained through different hierarchical paths.

3. Query string (query string)

The content in the query string is a key-value pair structure, and keythe valuevalue and number of sums are completely up to the programmer.

In the actual development process, we can use this method to customize and transmit the information we need to the server.

Extension: URL encode

Some characters in the URL have special meanings. For example, characters like /, ?, :etc. have been understood by the URL as special meanings, so these characters cannot appear randomly.

But what should we do if these special characters are required in a parameter? At this time, we must first escape these special characters.

A Chinese character UTF-8or GBKsuch an encoding method has no special meaning in the URL, but it still needs to be escaped; otherwise, the browser may regard a certain byte in UTF-8the or GBK encoding as a special symbol in the URL.

URL encodeThe escaping rules are as follows: convert the character to be transcoded into hexadecimal, then take 4 digits from right to left (less than 4 digits are processed directly), make a digit for every 2 digits, add % in front, and encode it into %XY format .

transcoding tool use image-20230523115638512

Expansion: The URL can be omitted and note instructions

The four most critical parts of the URL are IP address, port number, hierarchical file path, and query string.

The protocol name, login information, IP address/domain name, port number, hierarchical file path, query string, and fragment identifier in the URL can generally be omitted.

If the protocol name is omitted, it will default to http://, and if the port number is omitted, the browser will automatically determine which port to use according to the protocol type.

It is equivalent to omitting the file path with hierarchy /. Some servers will automatically be accessed when the / path is found /index.html.

In some cases, the IP address/domain name in the URL can be omitted, for example:

  1. When accessing resources on the local host, you can omit the IP address or domain name, and directly use localhost or 127.0.0.1 to access.
  2. When accessing other resources under the same domain name, you can omit the IP address or domain name and directly use the relative path to access.

However, in most cases it is best not to omit IP addresses or domain names for accuracy and reliability.

Note: The above two situations occur locally or under the same domain name. If they occur between different servers, the IP address/domain name cannot be omitted.

3.3, request header

The request header is mainly used to transmit additional information to the server, such as the data type that the client can receive, the compression method, the language, and the URL address of the page to which the hyperlink that sends the request belongs. When the browser makes a request to the server, the request headers sent are different according to the different functional requirements.

Each request header is composed of the header field name and value. The header field name and value are separated by a colon, and each request header is terminated with a carriage return and line feed symbol.

It should be noted that header field names are not case-sensitive, and it is customary to capitalize the first letter of a word.

Commonly used request header fields:

1,Accept

The Accept field is used to indicate the MIME (Multipurpose Internet Mail Extensions) types that the client program (usually a browser) can handle.

Example of use: If both the browser and the server support images of PNG type, the browser can send an included image/pngAccept field. When the server checks that the Accept header contains image/pngthis MIME type, PNG can be used in the img element of the web page type of file.

If there is a non-PNG image file, how will the server handle it? Will the file be converted? Or not deal with it?

In fact, the server will not convert files, that is, it will not convert non-PNG images into PNG types. The server will select the appropriate file to send to the client according to the MIME type specified in the Accept field. If If the client cannot handle the MIME type, it may display that the file cannot be opened or downloaded.

The MIME types that can be used as the value of the Accept field are as follows:

1) Accept:text/html: Indicates that the client wants to receive HTML text

2) Accept:image/gif: indicates that the client wants to receive resources in GIF image format

3) Accept:image/*: Indicates that the client can receive all image format subtypes

4) Accept:*/*: Indicates that the client can receive content in all formats

2,Host

The Host field is used to specify the host number and port number where the resource is located, and the value content may be the same as the URL or part of the URL.

In HTTP1.1, each request message sent by browsers and other clients must contain the Host request header field, so that the Web server can distinguish the virtual Web site that the client wants to visit according to the host name in the Host header field. When a browser accesses a Web site, it will automatically generate the corresponding Host request header according to the URL address in the address bar.

3,Content-Length,Content-Type

Content-Length is used to indicate the data length of the request body; Content-Type is used to indicate the data format of the request body.

The POST request has a transmission entity, so there is a request body, and the two fields Content-Length and Content-Type exist in the request header.

GET requests can also have a request body, but it is not commonly used. At this time, there will also be two fields, Content-Length and Content-Type, in the request header.

Content-Type is not only used to indicate the data format of the request body, but also used to indicate the data format of the response body.

The common options of Content-Type are:

1) application/x-www-form-urlencoded: form The data format submitted by the form.

The corresponding request body format is as follows:

image-20230523163102438

2) multipart/form-data: Form submission data format, added in the form tag enctyped="multipart/form-data", usually used to submit pictures/files.

The corresponding request body format is as follows:

image-20230523163149623

3) application/json: The data is in json format.

The corresponding request body format is as follows:

image-20230523163209379

4,User-Agent

The User-Agent field is referred to as UA (User Agent), which is used to describe the operating system and version information, browser and version information, etc. used by the specified browser or other client programs.

The User-Agent field allows the server to return different content for different types of browsers. For example, if the server checks this field and finds that the client is a wireless handheld terminal, it returns a WML document, and if it finds that the client is a common browser, it returns an HTML document.

image-20230523164547277

Among them, Windows NT 10.0; Win64; x64represents operating system information, and AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36represents browser information.

5,Refer

The Referer field is used to indicate which page this page was redirected from.

The request sent by the browser to the server can be sent directly by entering the URL in the address bar of the browser or clicking the favorite link, or by clicking a hyperlink in the web page.

The request sent through the first method does not contain the Referer field, while the request sent through the second method contains the Referer field.

The Referer field is often used by website administrators to track how website visitors navigate into the website, and it can also be used as an anti-leech link for the website.

Expansion: hotlink and anti-hotlink

Hotlinking refers to displaying some content that does not exist on your own server on your own page. By using tags in HTML files to link to resources on other websites, you can then obtain other people's resources and bypass other people's resource display pages. , Displayed directly on your own page for viewers to view. Generally, resources such as pictures, music, and videos are hotlinked.

Hotlinking can reduce the burden on your own server, but it increases the burden on the server of the hotlinked website and damages its legitimate rights and interests. Then the anti-theft chain appeared.

In order to protect its own resources from being hotlinked, the website can detect where the link to the current page or resource is from through the Referer field. Once it is detected that the access is not through the link of this site, it can block the access or jump to the specified page .

6,Cookie

The Cookie field is used in the HTTP request message to send Cookie information to the server. The format of the Cookie field is: Cookie: name1=value1; name2=value2; name3=value3.

What is the cookie information? Cookie refers to a data file that the website server stores on the user's local computer through the client browser. The browser stores the cookie on the user's computer and sends it back to the server in subsequent requests to track the user. Activity on the Site and storing user preferences. A cookie usually contains a name, a value, and an expiration time. When a user visits a website, the server will read the cookie file to obtain user information and provide personalized services.

The Cookie field in the request header contains the Cookie information already stored by the client, and the Set-Cookie field in the response header is used to send new Cookie information to the client.

The specific fields are described as follows:

  • Cookie: Used to send the cookie information stored by the client to the server in the HTTP request, in the format of "name=value".
  • Set-Cookie: Used to send new Cookie information to the client in the HTTP response, the format is "name=value;".

3.4, request body

The content format of the request body is closely related to that in the request header Content-Type. The common content formats of the body are as follows:

1,application/x-www-form-urlencoded

application/x-www-form-urlencoded: form The data format submitted by the form. The corresponding request body format is as follows:

image-20230523163102438

2,multipart/form-data

multipart/form-data: Form submission data format, added in the form tag enctyped="multipart/form-data", usually used to submit pictures/files.

The corresponding request body format is as follows:

image-20230523163149623

3)application/json

multipart/form-data: The data is in json format. The corresponding request body format is as follows:

image-20230523163209379

4. HTTP response

4.1, HTTP status code

The status code describes the result of the HTTP response status, that is, whether the response was successful and the reason for the failure.

HTTP status codes can be divided into five categories, namely information, success, redirection, request failure, and server error.

1. Information, the status code means that the request has been accepted and needs to be processed.

  • This type of response is provisional, containing only a status line and some optional response header information, and ends with a blank line.

2. Success, the status code means that the request has been successfully received, understood, and accepted by the server.

3. Redirection, the status code means that the client needs to take further action to complete the request.

  • Usually, these status codes are used for redirection, and the subsequent request address (redirection target) is specified in the Location field of this response.

4. The request failed, and the status code indicates that an error may have occurred on the client side, which hindered the processing of the server.

  • Unless the response is a HEAD request, the server SHOULD return an entity explaining the current error condition, and whether this is a temporary or permanent condition.

5. Server error, the status code means that the server has an error or abnormal state in processing the request, or realizes that the processing of the request cannot be completed with the current hardware and software resources.

HTTP status code table:

image-20230525162959572

Common HTTP status codes:

1, 200 OK : Indicates that the request is successful and belongs to the normal state.

2, 301 Moved Permanently : Permanent redirection, used to indicate that the requested URL has been permanently moved to a new URL, and cannot be restored to the original URL address in the future.

  • When receiving such a response, the client will automatically jump to the new URL address, and all subsequent access requests will be automatically changed to the new URL address.

3, 302 Move Temporarily : Temporary redirection, used to indicate that the requested URL has been temporarily moved to a new URL, but may return to the original URL address in the future.

  • When the client accesses the original URL, the server will return a 301 status code and a new URL address, and the client will automatically jump to the new URL address.

4, 403 Forbidden : The resource requested by the client is denied access by the server because the client does not have permission to access the resource.

5, 404 Not Found : The resource requested by the client does not exist on the server, usually because the URL address requested by the client is wrong or the resource has been deleted or moved.

6, 500 Internal Server Error : An internal error occurred while the server was processing the client request, which prevented the server from completing the request.

7, 504 Gateway Timeout : When the client sends a request to the server, the server does not respond after a period of time, resulting in a gateway timeout error.

Extension: the essential difference between redirection and request forwarding

Redirection and request forwarding are two commonly used jump methods in web servers

Redirection: Redirection means that after the server receives the client request, it redirects the requested resource address to another address. The client will re-initiate a new request to obtain new resources. Therefore, the essence of redirection is an interaction between the client and the server. The client needs to re-establish the connection, that is, the client needs to send two requests, so redirection will increase network delay and server burden.

Request forwarding: Request forwarding means that after receiving a client request, the server forwards the request to another address, and the server at another address processes the request and returns the resource. The client does not know that the request was forwarded to another server. Therefore, the essence of request forwarding is an interaction between servers, and the client only needs to send a request once, so request forwarding will reduce network delay and server burden.

In general, both redirection and request forwarding can realize the jump function, but the essential difference between them is whether the connection needs to be re-established.

If you need to jump across domains, or need to forward the request to a different server for processing, it is recommended to use request forwarding.

If you simply need to redirect the request to another address on the same server, redirection is recommended.

Redirect the actual schematic:

image-20230525211921953

Request to forward the actual schematic:

image-20230525213510801

4.2, response header

The basic format of the HTTP response header is basically the same as that of the request header, and the header fields are also basically the same.

Common values ​​of Content-Type in the response are as follows:

1. text/html: body data format is HTML

2. text/css: body data format is CSS

3. application/javascript: body data format is JavaScript

4. application/json: body data format is JSON

4.3, response body

The content format of the response body is closely related to the response header Content-Type. The common content formats of the body are as follows:

1. text/html: body data format is HTML

image-20230525174300370

2. text/css: body data format is CSS

image-20230525174349805

3. application/javascript: body data format is JavaScript

image-20230525174524974

4. application/json: body data format is JSON

image-20230525174441734

Summarize

The above is all the basic content of HTTP, I hope it can be helpful to everyone. If you have any questions that cannot be solved, please leave a message in the comment area or send me a private message. If you feel that it is useful to you, you can like it or pay attention to encourage bloggers, I will do better and better, thank you for your support, see you in the next issue!

insert image description here


Guess you like

Origin blog.csdn.net/m0_64338546/article/details/131147497