Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use HTTPclient to maintain a long connection

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report