What you need to know about HTTP

1, HTTP

1.1 Introduction

HTTP concept

HyperText Transfer Protocol, Hypertext Transfer Protocol, stipulates the relationship between the browser and the serverData transfer rules

  • The rules of data transmission refer to the request data and response data that need to be transmitted in the specified format.
  • If you want to know the specific format, you can open the browser, clickF12 to open the developer tools, and clickNetwork to view the request data of a certain request. and the specific format content of the response data, as shown in the figure below:

Insert image description here

Note: If you cannot see the above content in the browser, you need to clear the browser's browsing data. Chrome browser can use ctrl+shift+Del to clear.

Therefore, learning HTTP mainly means learning the specific format content of request and response data.

HTTP protocol characteristics

The HTTP protocol has some characteristics of its own, namely:

  • Based on TCP protocol: connection-oriented, secure

    TCP is a connection-oriented (three-way handshake is required before establishing a connection), reliable, byte stream-based transport layer communication protocol, which is more secure in data transmission.

  • Based on the request-response model: one request corresponds to one response

    Requests and responses have a one-to-one correspondence

  • The HTTP protocol is a stateless protocol: it has no memory for transaction processing. Each request-response is independent

    Stateless means that after the client sends an HTTP request to the server, the server responds with data according to the request. After the response, no information will be recorded. This feature has advantages and disadvantages,

    • Disadvantages: Data cannot be shared between multiple requests
    • Advantages: fast

    Problems caused by being unable to share data between requests, such as:

    • Jingdong Shopping, 加入购物车 and 去购物车结算 are two requests,
    • Due to the stateless nature of the HTTP protocol, after the response to the add-to-shopping-cart request is completed, the item added to the shopping-cart is not recorded.
    • After initiating a request to check out the shopping cart, the data cannot be displayed correctly in this request because it cannot obtain which products have been added to the shopping cart.

    When using specifically, we found that JD.com can display data normally. The reason is that Java has already considered this problem and proposed the use of 会话技术(Cookie、Session) to solve this problem.

1.2 Request data format

1.2.1 Format introduction:

The request data is divided into three parts, namelyrequest lineRequest headerRequest body

Insert image description here

  • Request line: The first line of data in the HTTP request. The request line contains three pieces of content, namely GET [request method] / [request URL path] HTTP/1.1 [HTTP protocol and version]

    There are seven request methods, the most commonly used are GET and POST.

  • Request header: Starting from the second line, the format is key: value format

    The request header will contain several attributes. Common HTTP request headers are:

    Host: 表示请求的主机名
    User-Agent: 浏览器版本,例如Chrome浏览器的标识类似Mozilla/5.0 ...Chrome/79,IE浏览器的标识类似Mozilla/5.0 (Windows NT ...)like Gecko;
    Accept:表示浏览器能接收的资源类型,如text/*,image/*或者*/*表示所有;
    Accept-Language:表示浏览器偏好的语言,服务器可以据此返回不同语言的网页;
    Accept-Encoding:表示浏览器可以支持的压缩类型,例如gzip, deflate等。
    

    What is this data used for?

    For example: the server can obtain relevant information about the client based on the content in the request header. With this information, the server can handle different business needs, such as:

    • The results of parsing HTML and CSS tags in different browsers will be inconsistent, so the same code will have different effects in different browsers.
    • The server obtains the client's browser type based on the data in the client's request header, and can set different codes according to different browsers to achieve a consistent effect.
    • This is what we often call browser compatibility issues
  • Request body: The last part of the POST request, which stores the request parameters

Insert image description here

As shown in the figure above, the content of the red line box is the content of the request body. There is a blank line separating the request body and the request header. At this time, the browser is sending a POST request. Why can't GET be used? At this time, you need to review the difference between GET and POST requests:

  • The request parameters of GET request are in the request line and there is no request body. The request parameters of POST request are in the request body.
  • There is a limit to the request parameter size for GET requests, but not for POST.

小结:

  1. The request data contains three parts, namely request line, request header and request body.

  2. POST request data is in the request body, GET request data is on the request line

1.3 Response data format

1.3.1 Format introduction

The response data is divided into three parts, namelyresponse lineresponse headerresponse body

Insert image description here

  • Response line: The first line of response data. The response line contains three pieces of content, namely HTTP/1.1 [HTTP protocol and version] 200 [response status code] ok [description of status code]

  • Response header: Starting from the second line, the format is key:value format

    The response header will contain several attributes. Common HTTP response headers are:

    Content-Type:表示该响应内容的类型,例如text/html,image/jpeg;
    Content-Length:表示该响应内容的长度(字节数);
    Content-Encoding:表示该响应压缩算法,例如gzip;
    Cache-Control:指示客户端应如何缓存,例如max-age=300表示可以最多缓存300秒
    
  • Response body: The last part. Store response data

    In the picture above...this part is the response body, which is separated from the response header by a blank line.

1.3.2 Response status code

Regarding response status codes, we first mainly understand three status codes, and we will master the rest when they are used later:

  • 200 ok client request successful
  • 404 Not Found The requested resource does not exist
  • 500 Internal Server Error An unexpected error occurred on the server side.

summary

  1. The response data contains three parts, namely response line, response header and response body.

  2. Understand the meanings of the three response status codes of 200, 404, and 500. The distribution is success, the accessed resource does not exist, and service errors.

Insert image description here

Guess you like

Origin blog.csdn.net/m0_69383623/article/details/128492461