- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的测试应用程序中,我向具有 Apache HttpClient 的同一主机执行连续的 HttpGet 请求,但在每次下一个请求时,结果表明前一个 HttpConnection 已关闭,新的 HttpConnection HttpConnection 已创建。
我使用相同的 HttpClient 实例并且不关闭响应。我从每个实体中获取 InputStream,使用 Scanner 从中读取,然后关闭 Scanner。我已经测试了 KeepAliveStrategy,它返回 true。请求之间的时间不超过 keepAlive 或 connectionTimeToLive 持续时间。
谁能告诉我这种行为的原因是什么?
已更新
我找到了解决方案。为了使 HttpConnecton 保持 Activity 状态,有必要在构建 HttpClient 时设置 HttpClientConnectionManager。我使用过 BasicHttpClientConnectionManager。
ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context)
{
long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1)
keepAlive = 120000;
return keepAlive;
}
};
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
try (CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager) // without this setting connection is not kept alive
.setDefaultCookieStore(store)
.setKeepAliveStrategy(keepAliveStrat)
.setConnectionTimeToLive(120, TimeUnit.SECONDS)
.setUserAgent(USER_AGENT)
.build())
{
HttpClientContext context = new HttpClientContext();
RequestConfig config = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setSocketTimeout(10000)
.setConnectTimeout(10000)
.build();
context.setRequestConfig(config);
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = httpClient.execute(httpGet, context);
HttpConnection conn = context.getConnection();
HttpEntity entity = response.getEntity();
try (Scanner in = new Scanner(entity.getContent(), ENC))
{
// do something
}
System.out.println("open=" + conn.isOpen()); // now open=true
HttpGet httpGet2 = new HttpGet(uri2); // on the same host with other path
// and so on
}
更新 2
一般来说,使用 conn.isOpen()
检查连接并不是检查连接状态的正确方法,因为:“内部 HTTP 连接管理器使用 ManagedHttpClientConnection 的实例作为代理管理连接状态和控制 I/O 操作执行的真实连接。如果托管连接被释放或被其消费者显式关闭,底层连接将与其代理分离并返回给管理器。即使服务消费者仍然持有对代理实例的引用,它不再能够执行任何 I/O 操作或更改真实连接的状态,无论是有意还是无意。” ( HttpClent Tutorial )
正如@oleg 指出的那样,跟踪连接的正确方法是使用 logger .
最佳答案
首先,您需要确保您正在使用的远程服务器支持保持 Activity 连接。只需简单地检查远程服务器是否在每个响应中返回 header Connection: Keep-Alive
或 Connection: Closed
。对于 Close
案例 there is nothing你可以这样做。您可以使用 this online tool执行此类检查。
接下来,您需要实现 ConnectionKeepAliveStrategy
,如 this manual 的第 #2.6 段中定义的那样.请注意,您可以使用现有的 DefaultConnectionKeepAliveStrategy
since HttpClient version 4.0 , 这样您的 HttpClient
将按如下方式构造:
HttpClient client = HttpClients.custom()
.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
.build();
如果服务器支持,这将确保您的 HttpClient
实例将通过保持 Activity 机制重用相同的连接。
关于java - 如何使用 Apache HttpClient 获得持久的 HttpConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46053067/
我正在为 BB 开发一个项目。该应用程序与网络一起工作,并通过 HTTP 发送/接收数据。现在我使用队列和队列管理器。管理器从一个后台线程开始,并在 while (true) 循环中工作,检查队列中是
我正在尝试使用以下代码从 Web 服务下载一个大型 (11MB) JSON 文件: public static void downloadBigFile(final String serverUrl,
我有这种方法可以使用 httpconnection 发布。我收到错误 sun.security.provider.certpath.SunCertPathBuilderException: 连接到自签
我想关闭 get 请求连接,但它没有关闭。 这是我的代码块 编辑代码块但不再起作用.. try { HttpClient client = new DefaultHttpCl
以下是我的android java代码的一部分,我收到错误“无法实例化 HttpConnection 类型” import org.apache.http.HttpConnection; import
我需要使用http连接调用服务get,响应包含阿拉伯字符,但是当我使用下面的代码调用它时 try { HttpURLConnection conn = (HttpURLConnect
我有以下代码: HttpConnection httpCon = null; InputStream fileIn = null; try { httpCon = (HttpConnecti
我想处理来自网站的数据。我需要处理 session ,所以我需要发送cookie。但是,如果我的程序等待超过 60 秒来请求新站点,DNS 解析器将为我带来一个新的 IP,以便我的程序将连接到另一台没
我正在为 GPRS 调制解调器 (Siemens TC65) 开发一个 java 应用程序。我多次调用一种方法来对不同的 URL 执行 HttpConnection。有时它工作正常,但有时我会被我的运
我编写了一个辅助函数,可以从 URL 检索内容。此函数还可以解析参数的 Map 并根据请求提供 url 或正文,具体取决于 GET 或 POST 方法。假设这个函数还可以做其他事情(更改 header
我使用此代码发送彩信,所有参数都正确,但连接被拒绝。并且网络已在移动彩信上连接。 HttpUtils 是一个来自此处下载的库的类:https://github.com/android/platform
我正在尝试使用 HTTPConnection (2.7.8) 发出请求,并且我已使用 HTTPConnection(host, timeout=10) 将超时设置为 10 >。但是,HTTPConne
在 J2ME 中,哪种连接类型更好?Get 还是 post。哪种连接速度更快?哪种使用的带宽更少?大多数手机都支持哪种连接类型?两者的优缺点是什么? 最佳答案 另请参阅 Is there a limi
我喜欢将文件上传到 http 服务器。这是我到目前为止的代码。问题是文件根本没有上传。代码有什么问题吗? 更新:我已经解决了。任何遇到同样问题的人,也许这段代码可能会有所帮助。 try{ Fil
这个问题已经有答案了: How can I fix 'android.os.NetworkOnMainThreadException'? (65 个回答) 已关闭 3 年前。 使用已接受的答案 her
我是 java 和 android 编程新手,通过遵循一些教程,我尝试制作一个 Android 应用程序,该应用程序必须向 php 页面发出 Http 请求以读取输出字符串。我在 doInBackgr
我们经常使用 HttpURLConnection API 向同一提供商调用 REST API(一种聚合用例)。我们希望保持一个包含 5 个连接的池始终对提供商主机开放(始终使用相同的 IP)。 什么是
HttpConnection 中的方法 setAllowUserInteraction() 在内部如何工作?这种情况下用户交互的意义是什么? 最佳答案 它应该表明,如果需要,系统可以要求用户提供额外的
第一次在这里问问题,而且是 android 编程的新手。我正在按照在线 youtube 教程创建用户“Tonikami TV”的登录名。除了 serverRequests 类之外,应用程序的一切都很好
我正在使用 Httconnection 连接到网络服务器,有时请求失败导致 EOFException 调用 httpconnection.getResponseCode() 时。 我在建立连接时设置了
我是一名优秀的程序员,十分优秀!