views:

44

answers:

1

I'm using apache httpclient 4.0 to connect to a video stream (motion jpeg) over http. Here's my code:

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));

The problem is that the client.execute() line seems to make hundreds of connections to the video stream. I can see this by logging onto the server and doing a netstat: there's an enourmous number of connections to port 80 and they're all stuck in the TIME_WAIT state.

Am I doing something wrong here? What's going on?

Thanks for the help.

A: 

That's what happens if you do not release connections back to the pool and / or create a new connection pool for each and every request.

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

oleg