我在尝试让 Android 应用程序(好吧,服务,它的情况下它有任何区别)使用持久 HTTP 1.1 连接时遇到问题。
以下循环(简化的测试用例)通过桌面 JRE 上的单个 TCP 会话进行工作,但在 Android 设备上会导致整个套接字创建/拆卸循环。
while (true) {
URL url;
try {
url = new URL("http://10.0.0.125:8080/SRV?");
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
Oracle 的 JDK 描述了一种称为“系统属性”的东西:
http.keepAlive=
默认值:true
http.maxConnections=
默认值:5
Android 运行时中是否有类似的东西阻止维持持久连接?
Android 的 JVM 在底层使用 Apache HTTP 组件库进行 HTTP 连接(甚至是使用 java.net 接口完成的连接):因此其行为与 Oracle JVM 略有不同。
理论上,底层 Harmony 代码尊重http.keepAlive
系统属性,但我不确定 Google 的副本是否保留该行为。
如果您想绝对确定发生了什么,则必须使用 HttpComponents 代码。这是漫长而痛苦的,但如果你看看http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html它概述了 http 组件的连接管理方法。请参阅第 2.11 节,其中详细介绍了如何使用 HTTP 组件显式控制连接管理。
祝你好运。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)