Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use HTTPclient to maintain a long connection

Shulou Source: shulou.com Published: 2022-06-03 01:33:05 10月28日 Update

This article introduces the knowledge of "how to use HTTPclient to maintain a long connection". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

HTTPclient maintains a long connection

First, explain what a long connection is.

How to implement a long connection in java?

Httpclient hangs the connection because it keeps the permanent long connection.

After adding the policy, the problem is resolved

HTTPclient keep persistent connection first explain what a persistent connection is.

When we make a request to a server, we need to establish a channel with the other party to transmit data, the so-called short connection, that is, we establish the channel, and then destroy the channel after transferring the data. Re-establish the channel next time you need it.

Long connection, that is, we have established a channel, after transmitting the data, do not destroy it first, and then reuse this channel next time if we still need to transmit data.

Because the establishment of a channel takes time, the advantage of a long connection is that it responds quickly, but the server is under great pressure, because at the same time, many people are setting up a channel to the server, even if some channels have finished transmitting data. because of the long connection, the channel will not be destroyed; for short connection, the response speed is slow and the pressure on the server is low.

Since more emphasis is placed on the user experience, long connections are now the most common.

How to implement a long connection in java?

It's really simple, just add a specific parameter to the request header of the request: "Connection": "keep-alive". In this way, if the other party supports a persistent connection, then the connection will remain persistent.

Here comes the crux of the problem. In a code that tests the response speed of a https request, I found that when the response data of the other party is null, that is, when the data in responseBody is null, the response speed is very fast, about 5ms, but once the other party returns the response data, the response may reach 20ms.

Then ask the operation and maintenance colleagues to grab the package and find that each connection will spend time on user authentication, which reflects from some aspect that a new connection is established every time.

HttpPost httpPost = new HttpPost ("xxxxx"); httpPost.addHeader ("Connection", "keep-alive"); CloseableHttpClient httpClient = null;for (int I = 0; I

Tags: Data channel code each other speed problem server service that is source code policy transmission success object time more knowledge permanence interpretation ordinary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi OPPO Reno Redmi MySQL Docker