Java EE Basics (03): http request details, handshake and wave process introduction

1、 Introduction to HTTP protocol

1. Concept description

HTTP hypertext transmission protocol is a transmission protocol used to transmit hypertext from the world wide web server to the local browser. It transmits data based on TCP / IP communication protocol: HTML files, pictures, query data, etc. HTTP protocol is based on client server architecture mode. As an HTTP client, the browser sends a request to the server, that is, the web server, through a URL. According to the received request, the web server sends response information to the client after processing the request.

2. Protocol features

When requesting the server, only the request method and path need to be transmitted. The request type is usually get and post. Due to the simple HTTP protocol, the program scale of HTTP server is small, so the communication speed is very fast.

HTTP allows the transfer of any type of data object. The type being transferred is marked by content type.

Connectionless means that only one request can be processed per connection. After the server processes the customer's request and receives the customer's response, it disconnects. In this way, the transmission time can be saved.

HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory for transaction processing. If the status is missing, the previous information is required for subsequent processing, and if not, a new request is required, which may increase the amount of data transmitted each connection.

2、 HTTP request details

1. Request interface

public class ServletOneImpl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException {
        System.out.println("doGet...");
        response.setContentType("text/html;charset=utf-8");
        String userName = request.getParameter("userName") ;
        response.getWriter().print("Hello:"+userName);
    }
    @Override
    protected void doPost(HttpServletRequest request,IOException {
        System.out.println("doPost...");
        response.setContentType("text/html;charset=utf-8");
        String userName = request.getParameter("userName") ;
        response.getWriter().print("Hello:"+userName);
    }
}

2. Request content

Address: http://localhost:6003/servletOneImpl?userName=cicada

GET /servletOneImpl?userName=cicada HTTP/1.1
Host: localhost:6003
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
DNT: 1
Referer: http://localhost:6003/request.jsp
Accept-Encoding: gzip,deflate,br
Accept-Language: zh-CN,zh;q=0.9
Cookie: JSESSIONID=183C0F91B49A025C795FBC3067B37BC8

Address: http://localhost:6003/servletOneImpl

POST /servletOneImpl HTTP/1.1
Host: localhost:6003
Connection: keep-alive
Content-Length: 15
Cache-Control: max-age=0
Origin: http://localhost:6003
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) 
Accept: text/html,zh;q=0.9
Cookie: JSESSIONID=183C0F91B49A025C795FBC3067B37BC8

3. Parameter description

4. Difference between get and post

From the browser's point of view, the difference between the two requests is that the get method reads resources, such as get to a static page. Even if multiple reads will not affect the access data, it is also called "idempotent" operation. Post defines the form in the page. Submitting the form will submit the data to the server, and in most cases, it will generate data. For example, the commonly used data saving interface is not an "idempotent" operation, which means that it cannot be executed multiple times at will.

This refers to the type of request submitted by requesting the service interface with Ajax program. Or other HTTP request tool classes, as well as requests between various feign interfaces in microservices. In this case, there are relatively few restrictions when the interface sends requests. For example, rest style interfaces often use get, post, put and delete to obtain, create, update and delete resources respectively,

3、 HTTPS request protocol

1. Introduction to HTTPS

HTTPS: it is an HTTP channel based on security. It is a secure version of HTTP. SSL layer is added to HTTP requests. The security basis of HTTPS is SSL. Therefore, SSL is required for the details of encryption. In short, HTTPS protocol is a network protocol built by SSL + HTTP protocol, which can carry out encrypted transmission and identity authentication. It is safer than HTTP protocol.

The main functions of HTTPS protocol can be divided into two kinds: one is to establish an information security channel to ensure the security of data transmission; The other is to confirm the authenticity of the website.

2. Working principle of HTTPS

(1) The client accesses the server based on HTTPS and establishes SSL connection with the server; (2) After receiving the request, the server will send the certificate containing the public key to the client; (3) The client and server coordinate the security level of SSL connection, that is, the level of encryption; (4) The client establishes the session key according to the security level agreed by both parties, encrypts the session key with the public key and transmits it to the server; (5) The server decrypts the content transmitted in the session using the private key, and encrypts the communication with the client using the session key;

3. Difference between HTTPS and http

The HTTPS protocol needs to apply for a certificate from the ca. generally, there are few free certificates, so it requires a certain fee.

HTTP is a hypertext transmission protocol, information is transmitted in plaintext, and HTTPS is a secure SSL encrypted transmission protocol.

HTTP and HTTPS use completely different connection modes and different ports. The former is 80 and the latter is 443.

4、 TCP transport protocol

1. Introduction to TCP protocol

TCP transmission control protocol is a connection oriented, reliable and byte stream based transport layer communication protocol. TCP is intended to adapt to and support the hierarchical protocol hierarchy of multi network applications.

2. Three handshakes

This scene can be described as a call in life:

甲:你好,我是甲,你是乙吗;
乙:你好甲:我是乙;
甲:正好找你有点事情,身份确认:

The client actively initiates a connection request to the server, and sends syn = 1 in the request message. At this time, the initial serial number SEQ = x is randomly generated. At this time, the client process enters the syn-sent synchronization sent state.

After receiving the request message, the server confirms the syn of the customer. If the request is not rejected, it sends a confirmation message. In the message, ACK = 1, syn = 1, ACK = x + 1 and send a syn packet SEQ = y. at this time, the server process enters the syn-rcvd synchronization receiving state.

After receiving the confirmation, the client needs to confirm the ACK = 1 and ACK = y + 1 of the message to the server. At this time, the TCP connection is established and the client enters the established connection state. After three handshakes, the client and server start transmitting data.

3. Four waves

The client sends an end fin, which is used to actively close the data transmission with the server, release the connection and stop sending data. The message header: fin = 1, serial number SEQ = u; Then, the client enters the termination wait 1 state fin-wait-1.

The server receives this fin and sends a confirmation message ack = 1. The confirmation received serial number is the received serial number + 1, that is, ACK = u + 1, and carries its own serial number SEQ = v. like syn, a fin will occupy a serial number. In this way, the server informs the application process that the client has no data to send. If the server sends data, the client still needs to receive it. This state will last for a period of time, and the server enters the closed-wait state. After receiving the confirmation request from the server, the client enters the termination state fin-wait-2 and waits for the server to send the connection release message.

The server sends the release connection message fin = 1, ACK = u + 1 to the client. At this time, the server is still in the semi closed state, and the server may also send some data. At this time, the serial number is SEQ = W. in this way, the server enters the final confirmation state last-ack and waits for the confirmation of the client.

After receiving the connection release message from the server, the client sends back a confirmation, ACK = 1, ACK = w + 1, and the serial number is SEQ = u + 1. In this way, the client enters the time-wait state. At this time, the TCP connection has not been released, and the longest message segment life must pass before it enters the closed state. MSL: the longest message segment life, usually 2 minutes. When the TCP connection is released, the active party must go through 2msl before entering the closed state. Therefore, the active party closes late.

5、 Source code address

GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent
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
分享
二维码
< <上一篇
下一篇>>