What is the HTTP protocol? What is the difference between get and post requests? There are several content types for HTTP requests. What is the difference? What scenario is HTTP suitable for? What are the HTTP status codes?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [frequently asked questions] [r_301_2422 @ case] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[what is the HTTP protocol? What are the differences between get and post requests? What are the content types of HTTP requests? What scenarios are HTTP suitable for? What are the HTTP status codes?]

[java small class of Xiuzhen academy] what is the HTTP protocol? What is the difference between get and post requests? What are the content types of HTTP requests? What scenarios are HTTP suitable for? What are the HTTP status codes?

Hello, I'm Zhao Linai, a student of the fourth phase of Xi'an Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the knowledge points in deep thinking on Java task 2 on the official website of the Academy - what is HTTP protocol?

(1) Background:

The birth of HTTP

Planning the web for knowledge sharing

In March 1989, the Internet only belonged to a few people. In this dawn of the Internet, HTTP was born. Dr. Tim Berners Lee of CERN (European Organization for Nuclear Research) proposed an idea that researchers from far away places can share knowledge.

The original idea is: connect the world wide web (WWW) with hypertext formed by the correlation between multiple documents. Now, three www construction technologies have been proposed: HTML (Hypertext Markup Language) using SGML (Standard General Markup Language) as the text markup language of the page; HTTP as the document delivery protocol; Specify the URL of the address where the document is located (uniform resource locator).

WWW is the name of the client application used by the web browser to browse hypertext. Now it is used to represent the collection of this series, which can also be referred to as web

Stagnant http

HTTP came out in 1990. HTTP was not established as a formal standard at that time. HTTP now actually contains http1 Before 0,

Therefore, it is called http / 0.9

HTTP was officially published as a standard in May 1996. The version was named http / 1.0 and recorded in rfc1945. Although it is an initial standard, the protocol standard is still widely used on the server side.

HTTP/1.1

Http / 1.1 published in January 1997 is the mainstream version of HTTP protocol. The original standard was rfc2068, and the later revised version rfc2616 is the latest version.

It can be seen that HTTP, as a web document transfer protocol, has hardly been updated. A new generation of HTTP / 2.0 is being developed, but it still takes time to achieve high usage coverage.

The emergence of HTTP protocol in that year was mainly to solve the problem of text transmission. Because the protocol itself is very simple, many application methods are envisaged and put into practical application. The current HTTP protocol has gone beyond the limitations of the web framework and has been applied to various scenarios.

(2) Knowledge analysis:

What is the HTTP protocol?

HTTP protocol is used for communication between client and server. HTTP protocol is the same as many other protocols in TCP / IP protocol family, which is used for communication between client and server. The end requesting access to resources such as text or image is called the client, and the end providing resource response is called the server.

The communication must be established from the client first, and the server will not send a response until it receives the request.

Request message

The request message is composed of request method, request URI, protocol version, optional request header field and content entity.

response message

The response message is basically composed of protocol version, status code (digital code indicating success or failure of the request), reason phrase to explain the status code, optional response header field and entity body.

HTTP is a protocol that does not save state

HTTP is a stateless protocol that does not save state.

The htt protocol itself does not save the communication state between request and response. In other words, at the HTTP level, the protocol does not persist the sent request or response.

Using HTTP protocol, whenever a new request is sent, a corresponding new response will be generated. The protocol itself does not retain all previous request or response message information. This is to process a large number of transactions faster and ensure the scalability of the protocol. The HTTP protocol is designed to be so simple.

HTTP method to inform the server of the intention

Get: get resources

The get request is used to request access to a resource that has been identified by the URI. After the specified resource is parsed by the server, the response content is returned. In other words, if the requested resource is text, return it as it is; If it is a program like CGI (Common Gateway Interface), the output result after execution is returned.

Post: transfer entity principal

The post method is used to transfer the body of the entity

Although the get method can also be used to transfer the entity body, the get method is generally not used for transmission, but the post method is used.

Although the function of post is very similar to get, the main purpose of post is not to obtain the main content of the response.

HTTP is only the code of conduct, and TCP is the benchmark for how to implement get and post.

Difference: get generates one TCP packet, and post request generates two TCP packets

For get requests, the browser will send the HTTP header and data together, and the server will respond 200 (return data)

For post, the browser sends the header first, the server responds to 100 continue, the browser sends data, and the server responds to 200 OK (return data)

Persistent connections save traffic

In the initial version of the HTTP protocol, the TCP connection was disconnected once without an HTTP communication. In terms of the communication situation in that year, there was no big problem even if it was text transmission with small capacity. However, with the popularity of HTTP, more and more documents contain a large number of pictures.

For example, when using a browser to browse an HTML page containing many pictures, when sending a request to access the HTML page resources, it will also request other resources contained in the HTML page. Therefore, each request will cause fearless TCP connection establishment and disconnection, increasing the overhead of traffic.

Persistent connection:

In order to solve the above TCP connection problems, http / 1.1 and some http / 1.0 came up with a method of persistent connections (HTTP persistent connections, also known as HTTP keep alive or HTTP connection reuse). The specific purpose of persistent connections is to maintain the TCP connection state as long as either end does not explicitly propose to disconnect.

Persistent connection aims at the interaction of multiple requests and responses after establishing a TCP connection.

Pipelining: persistent connections make it possible for most requests to be pipelined. After the previous request is sent, you need to wait and receive a response before sending the next request. After the emergence of pipelining technology, the next request can be sent directly without waiting for a response. In this way, multiple requests can be sent in parallel at the same time without waiting for responses one by one.

The status code tells the request result returned from the server

The function of the status code is to describe the returned request result when the client sends a request to the server. With the help of the status code, the user can know whether the server has processed the request normally or an error has occurred.

The status code, such as 200 OK, is composed of 3 digits and reason phrase.

The first digit in the number specifies the response category, and the last two digits have no classification. There are five response categories:

one ×× The request accepted by the informational status code is being processed

two ×× The success status code request is normally processed

three ×× Redirection (redirection status code) requires additional operations to complete the request

four ×× Client error the server was unable to process the request

five ×× Server error (server error status code) server processing request error

Content type describes the media type of the object in the entity body.

Common media format types are as follows:

Text / HTML: HTML format

Text / plain: plain text format

Text / XML: XML format

Image / GIF: gif picture format

Image / jpeg: JPG picture format

Image / PNG: PNG picture format

(3) Frequently asked questions:

What are the common HTTP error codes?

(4) @ r_301_2422 @ case:

The response results beginning with 4 indicate that the client is the cause of the error

400 Bad Request

This status code indicates that there is a syntax error in the request message. When an error occurs, you need to modify the request content and send the request again.

401 Unauthorized

This status code indicates that the request sent needs authentication information that has passed HTTP authentication (basic authentication, digest authentication). In addition, if the request has been made once before, it indicates that the user authentication has failed.

403 Forbidden

This status code indicates that access to the requested resource was denied by the server. It is not necessary for the server to give a detailed reason for rejection, but if you want to explain, you can describe the reason in the entity part, so that users can see it.

Failure to obtain access authorization for the file system, some problems with access permissions (unauthorized sending source IP address attempts to access) and other listed situations may be the causes of 403.

404 Not Found

This status code indicates that the requested resource cannot be found on the server. In addition, it can also be used when the server rejects the request and does not want to explain the reason.

The response result starting with 5 indicates that an error has occurred in the server itself.

500 Internal Server Error

This status code indicates that an error occurred when the server executed the request. There may also be a bug or some temporary failure in the web application.

503 Service Unavailable

This status code indicates that the server is temporarily overloaded or in the process of shutdown maintenance, and is unable to process the request now.

Inconsistency between status code and status

Many returned status code responses are wrong, but you may not be aware of this. For example, when an error occurs inside a web application, the status code still returns 200 OK, which is often encountered.

(5) Coding practice:

Try to find what is used in real projects. If you can find a website using your knowledge, this is the best. Apply what you have learned, otherwise it will be meaningless to take it as an exercise. Prepare more demos and combine the knowledge points with the demo during the explanation to facilitate everyone to understand the knowledge points explained.

(6) Expand thinking:

Relationship between various protocols and HTTP protocol

We can understand the roles of IP protocol, TCP protocol and DNS services in the communication process using HTTP protocol through the following figure.

(7) References:

Books: illustrated http

Author: [Japan] Ueno Xuan

(8) More discussion:

Q1: rubrian: 1. Disadvantages of HTTP

a. The communication uses plaintext and is not encrypted, and the content may be eavesdropped

b. If the identity of the communicating party is not verified, it may be disguised

c. The integrity of the message cannot be verified and may be tampered with

Q2: Liu you: 2. Relationship between HTTP and http

A2: Zhao Linai: HTTPS is HTTP plus encryption (usually SSL secure communication line) + authentication + integrity protection

Q3: Qin Yonghui: 3. What is HTTP protocol

A3: Zhao Linai: http: hypertext transfer protocol. The reliable data transmission protocol is used, which will not be damaged or confused in the process of transmission. HTTP can quickly, conveniently and reliably move various information blocks from web server providers all over the world to the web browser on people's desktop.

(9) Thanks:

Thanks to elder martial brother Liu you. This tutorial is based on their previous technology sharing.

(10) Conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>