Apache HttpClient MultiThread

HttpParams

HttpParams params = new BasicHttpParams();
SchemeRegistry registry = new SchemeRegistry();

.register(new Scheme(“http”, 8000, new PlainSocketFactory()));
cm = new ThreadSafeClientConnManager(registry);
client = new DefaultHttpClient(cm, params);

example.

HttpPost post = new HttpPost(pushURL);
	
FileBody fileBody = new FileBody(f);	
MultipartEntity reqEntity = new MultipartEntity();	
reqEntity.addPart(mcidFileName, fileBody);	
post.setEntity(reqEntity);



HttpParams params = new BasicHttpParams();	
SchemeRegistry registry = new SchemeRegistry();



registry.register(new Scheme("http", 8000, new PlainSocketFactory()));

// deprecated

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

ClientConnectionManager cm = new ThreadSafeClientConnManager(registry); 	
HttpClient client = new DefaultHttpClient(cm, params);	
client.getParams().setParameter("http.connection.timeout", 1000);	
client.getParams().setParameter("http.socket.timeout", 1000);	
client.getParams().setParameter("http.protocol.version",HttpVersion.HTTP_1_1);	
client.getConnectionManager().closeIdleConnections(2000,TimeUnit.MILLISECONDS);

try {

    client.execute(post);

    System.out.println(name + " send Ok!!");

} catch (Exception e1) {

    System.out.println(name + " // " + e1);

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s