gpt4 book ai didi

java - HTTPClient 实例是否不适合重用?

转载 作者:行者123 更新时间:2023-12-01 17:26:17 24 4
gpt4 key购买 nike

为每个请求重复使用单个 HTTPClient 实例是一种不好的做法吗?我发出请求以从 yahoo webservice 获取数据,现在在另一个页面中我向 google 发出 webservice 调用。

我可以在这两个不同的页面中使用相同的 HTTPClient 实例还是应该为这两个页面创建新的 HTTPClient 对象?

最佳答案

您绝对应该重用它们并将HttpClient视为单例。在整个应用程序中重用单个实例可以利用保持 Activity 连接并在调用之间保留 cookie。

引用official documentation对于 3.x:

Generally it is recommended to have a single instance of HttpClient per communication component or even per application. However, if the application makes use of HttpClient only very infrequently, and keeping an idle instance of HttpClient in memory is not warranted, it is highly recommended to explicitly shut down the multithreaded connection manager prior to disposing the HttpClient instance. This will ensure proper closure of all HTTP connections in the connection pool.

这同样适用于 4.x,只需确保使用 PoolingClientConnectionManager .

关于java - HTTPClient 实例是否不适合重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14791129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com