我正在尝试通过 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 响应不包含标头“连接:关闭”)。
请赐教!
干杯,安德烈