This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

preface

Before that, check out my previous post, which was also about protocols.

Do you really understand TCP/IP? Come in and take a look

What the hell is UDP

When opening a web page, we often notice a uniform http:// (or https://) in front of the address, indicating that the visit is using HTTP for communication.

So the question is: why do we use this protocol when we communicate?

Simply speaking, the so-called agreement is actually a specification, a standard, everyone to abide by. Through the use of unified norms, the communication parties can effectively structure the information and put the corresponding information in its place, which is exactly “God belongs to God and Caesar belongs to Caesar”. This can greatly reduce the cost of transmitting information.

Suppose what happens if we don’t follow a protocol when we transmit information across the Internet? The most direct impact is that of chicken and duck.

concept

HTTP, or HyperText Transfer Protocol, is the foundation on which the World Wide Web as we know it today, and is the most common type of Protocol we use to access the Internet.

agreement

In our daily life, we can also see agreements everywhere, for example, when looking for an apartment, we will sign a “rental agreement”

Protocols in real life are essentially the same as protocols in computers.

  • The word “coordination” means that there must be more than two participants. There are two players in a rental agreement: you and the landlord.
  • “Yi”, on behalf of the participants of a behavioral agreement and norms. The rental agreement stipulates the lease period, the monthly rent amount, how to deal with the default.

In the case of HTTP, we can think of it this way.

HTTP is a protocol used in the computer world. It establishes a specification for communication between computers (two or more actors) and associated controls and error handling (behavior conventions and specifications) in a language that computers can understand.

transmission

By “transfer,” we mean moving A bunch of stuff from point A to point B, or from point B to point A.

Don’t take this simple action lightly, it contains at least two important pieces of information.

HTTP is a two-way protocol.

When we surf the Internet, the browser is the requester A, baidu website is the answer B. The two sides agree to use HTTP protocol to communicate, so the browser sends the request data to the website, the website then returns some data to the browser, and finally rendered by the browser on the screen, you can see pictures and videos.

Although data is transmitted between A and B, it is allowed to have A relay or relay.

Just like the students in the first row want to pass the paper to the students in the last row, so the process of transmission will need to pass through many students (middlemen), so the transmission mode from “A < — > B” to “A <-> N <-> M <-> B”.

With HTTP, the middleman is required to comply with the HTTP protocol and can add anything extra as long as it doesn’t interrupt the basic data transfer.

In terms of transport, we can further understand HTTP.

HTTP is a convention and specification for transferring data between two points in the computer world.

hypertext

Text: In the early days of the Internet, it was simply character text, but now the meaning of “text” has expanded to include images, videos, compressed packages, etc., all of which are considered “text” in the eyes of HTTP.

Hypertext: It’s text that goes beyond normal text. It’s a mixture of text, pictures, video, etc. The key is hyperlinks, which jump from one hypertext to another.

HTML is the most common hypertext, it itself is pure text file, but with a lot of internal tags defined images, videos and other links, after the browser interpretation, presented to us is a text, a picture of the web page.

HTTP is a “convention and specification” for the “transfer” of text, pictures, audio, video and other “hypertext” data between “two points” in the computer world.

Format of the HTTP protocol

We divide the entire protocol into two broad categories: request and response.

The HTTP request

Generally, the HTTP protocol format is divided into four parts: start line, message header, blank line, message body; As shown in the figure.

The start line contains three pieces of information: method, URI, and HTTP protocol version.

Method: Refers to the action to be performed on this request, sometimes called “HTTP predicate” or “HTTP verb.” The common methods are GET and POST. GET indicates that the client needs to fetch resources from the server. POST means that the client wants the server to transfer some form data.

URI: Generally an absolute path followed by a question mark “?” And query strings; When using a proxy, it is a full URL.

HTTP version: indicates the HTTP version you are using to avoid confusion.

The most common form of the start line looks like this:

GET/user / 3940246036953293 HTTP / 1.1Copy the code

Becomes (the URL is fictitious) when using a proxy:

GET https://juejin.cn/user/3940246036953293 HTTP / 1.1Copy the code

Header: Contains some description of the message in the format

:

. Specifically, message headers are grouped into four broad categories: generic headers, request headers, response headers (used to respond to messages), and entity headers.

Blank line: indicates the end of the header and the beginning of the body. No more ink is needed.

Message body: The body of an HTTP message to be transmitted. Somewhat awkwardly, however, there are methods that don’t need to transmit any other information, just the start line and header (like the GET method), so not only is this part not necessarily the longest, but it may even be empty.

The HTTP response

Similar to the request message, the HTTP response message is divided into four parts: the status line, the header, the blank line, and the body, as shown in the figure below:

The last three parts are basically the same as the HTTP request message, so focus on the status line.

The status line also consists of three parts: HTTP protocol version, status code, and status text. There is no need to specify the HTTP version.

Status code: Actually we are familiar with it. The most typical one is that we get a 404 status code every time we visit a URL that doesn’t exist. So a status code is actually a number that identifies a successful request. In addition to 404, typical status codes include 200 (request successful), 301 (resource permanently moved), 302 (resource temporarily moved), and so on.

According to the first digit, the status code can be divided into five types:

Status text: it is a brief and pure piece of information, describing the actual state represented by the status code to facilitate human-computer interaction.

So a typical status line might look like this:

HTTP / 1.1 404 Not FoundCopy the code

When a client requests a web page from the server using the GET method, if the request is successful, the body of the server’s HTTP response message contains the HTML text of the web page.

conclusion

This article is only a brief introduction to the HTTP protocol, a superficial understanding of some of the knowledge related to it. We’ll take a closer look at HTTP in future articles. Follow me, a new era of migrant workers who focus on sharing Java knowledge.