具有Keep-Alive的Android DefaultHTTPConnection不会重用TCP连接

时间:2013-01-22 03:33:00

标签: android http post connection keep-alive

我正在尝试使用Keep Alive重用HTTP连接。服务器支持Keep Alive,我发送Connection:Keep-Alive HTTP标头,并且在收到响应后,连接仍然是FIN(由Android客户端发起)。一些帮助将不胜感激。

这里有一些代码:

DefaultHttpClient client = new DefaultHttpClient(); 
client.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
client.setReuseStrategy(new DefaultConnectionReuseStrategy());
HttpParams params = client.getParams();

ClientConnectionManager mgr = client.getConnectionManager();
client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);

HttpPost post = new HttpPost(URL);
post.setHeader("Connection","Keep-Alive");
ByteArrayEntity entity = new ByteArrayEntity(requestBundle.postString().getBytes());
entity.setContentType("application/xml");
post.setEntity(entity);
HttpResponse response = client.execute(post);

当我检查HTTP请求标头时,我看到了这些:

Connection: Keep-Alive\r\n
Content-Length: 459\r\n
Content-Type: application/xml\r\n
Host: xxx.yyy.com\r\n
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)\r\n

在HTTP响应标头中,我看到了这些:

HTTP/1.1 200 OK\r\n
Content-Type: application/xml\r\n
Content-Length: 8049\r\n
Date: Tue, 22 Jan 2013 03:14:40 GMT\r\n
Server: lighttpd/1.4.31\r\n

查看响应和其他TCP数据包通过线路,很明显Android客户端根据我不知道的一些HTTP客户端设置启动FIN,这不是由Keep-Alive设置引起的服务器(HTTP响应不包含标题“Connection:close”)。

请赐教!

干杯, 安德烈

0 个答案:

没有答案