800 words take you to understand Http request and response

Hello, I am Xiao Suoqi. Today I will share with you the requests and responses in the computer network. These are also essential in javaWeb.

Introduction to HTTP

HTTP is a protocol for passing data between web applications, and HTTP requests and responses are the basic units of communication between clients and servers. We can use a scene in life to compare how they work.

Just like you go to a restaurant to order food, an HTTP request is equivalent to ordering food from the waiter, telling them what you want to eat, what seasonings you need to add, and so on. In this process, the HTTP request includes four parts: request line, request header, blank line and request body. The request line contains information such as the request method, URL, and HTTP protocol version; the request header contains some additional information, such as the source of the request, the type of data accepted, etc.; the blank line is used to distinguish the separator between the request header and the request body; the request body contains The data content that the client wants to transmit to the server, such as form data, JSON data, etc.

After the waiter understands your request, he will take the information to the back kitchen and tell the chef, and let them cook the dishes according to your request. This process is equivalent to the server performing corresponding operations or returning corresponding data according to the HTTP request, and responding to the client. An HTTP response consists of three parts: a status line, response headers, and a response body. The status line contains information such as the response status code, status description, and HTTP protocol version; the response header contains some additional information returned by the server, such as the type of response content, encoding method, etc.; the response body is the actual data content returned by the server to the client , such as HTML pages, JSON data, etc.

Finally, when your dishes are ready, the waiter will deliver them to you and tell you the name, taste and other information of each dish. These are equivalent to the status line, response headers and response body in the HTTP response. You can judge whether the dish meets your requirements based on this information, and perform further operations, such as eating, packing, etc.

HTTP requests and responses are the means of communication between the client and the server, just like ordering food and waiter delivery. Through HTTP requests, the client can request resources (such as web pages, pictures, videos, etc.) from the server, and can also submit data (such as form data, file uploads, etc.) to the server. The server can return corresponding data or perform corresponding operations according to the request, so that the web application can run normally.

Detailed request and response

overview

The request message mainly includes three parts, the first part is the request line (method, path behind the server, http version - such as: GET /users HTTP/1.1 ), the second part is Headers (html, json, data... format), The third part is Body.

The response message also includes three parts, the first part is the status line, the second part is Headers, and the third part is Body.

The status line mainly includes three parts. The first one is the http version , and the commonly used one is 1.1. The second is the status code . The common ones are 200, which means success, and 404, which means that the content cannot be found. The third is status information . The specific format is as follows

HTTP/1.1 200 OK

HTTP request package (Request)

  • Request Line:

Including request method, access URL and its parameters, http protocol version number, etc.

For example:

GET /index.html HTTP/1.1
  • Request header (Request Header):

Contains client environment information, browser information, etc., and can also have various puzzling parameters.

For example:

Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

But in the early stage, we don't need to pay too much attention to these headaches

  • Blank Line:

There must be a blank line between the request header and the request data, this line is quiet and doesn't say anything.

  • Request Body:

It usually contains form data, etc. If it is a transfer file, then here is binary data.

HTTP response packet (​Response)

The specific format is as follows

  • Status Line:

The status line consists of three main parts. The first one is the http version , which is commonly used or 1.1. The second is the status code . The common ones are 200, which means success, and 404, which means that the content cannot be found. The third is status information . Among them, the status code can provide some very vivid feedback.

For example:

HTTP/1.1 200 OK
  • Response header (Response Header):

Contains the type, length, encoding format, etc. of the response data, as well as various magical avatars, such as "server".

For example:

Content-Type: text/html; charset=UTF-8
Server: Apache
Content-Length: 1234
  • Blank Line:

There must be a blank line between the response headers and the response data. This line is also quiet, but it conveys a lot of information.

  • Response Body:

The specific content returned by the server to the client, such as web page HTML code, image files, etc.

The above is our Http protocol~

Guess you like

Origin blog.csdn.net/m0_64880608/article/details/130333926