1

我正在使用 apache httpclient 4.0 通过 http 连接到视频流(运动 jpeg)。这是我的代码:

DefaultHttpClient client;

HttpParams params = new BasicHttpParams();

List<String> authpref = new ArrayList<String>();
authpref.add(AuthPolicy.DIGEST);
authpref.add(AuthPolicy.BASIC);

params.setParameter("http.auth.proxy-scheme-pref", authpref);
params.setParameter("http.protocol.handle-authentication", Boolean.TRUE);

SchemeRegistry schemeRegistry = new SchemeRegistry();

schemeRegistry.register(
    new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

ClientConnectionManager connectionManager =
    new ThreadSafeClientConnManager(params, schemeRegistry);

client = new DefaultHttpClient(connectionManager, params);

client.getCredentialsProvider().setCredentials(
     new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
     new UsernamePasswordCredentials(username, password));

HttpResponse videoResponse = client.execute(new HttpGet(url));

问题是 client.execute() 行似乎与视频流建立了数百个连接。我可以通过登录服务器并执行 netstat 看到这一点:有大量连接到端口 80,它们都卡在 TIME_WAIT 状态。

我在这里做错了吗?这是怎么回事?

谢谢您的帮助。

4

1 回答 1

2

如果您不将连接释放回池和/或为每个请求创建一个新的连接池,就会发生这种情况。

http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/connmgmt.html

于 2010-07-02T10:22:23.870 回答