2 Minutes To Code (β Version)

Apache Commons Http Client - 2 minute tutorial

Basic usage : How to use http client from multiple threads

This example shows how to use HttpClient in a multi-threaded scenario.

First of all you need an object of class org.apache.commons.httpclient.MultiThreadedHttpConnectionManager and pass it to the constructor of class org.apache.commons.httpclient.HttpClient. If required, proxy too can be set as shown below.

Then onwards, this http client can be used normally as shown in the artefact Worker.java. HttpClient is thread-safe and multiple threads can call its executeMethod(...) simultaneously.
package com.techfundaes.httpBag;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;

public class MultiThreadedHttp
{
	static HttpClient httpClient = null;
	
	public static void main(String[] args)
	{
		MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
		httpClient = new HttpClient(httpConnectionManager);
		httpClient.getHostConfiguration().setProxy("202.56.254.213", 5631);
		for(int i = 0; i < 10; i++)
		{
			WorkerThread workerThread = new WorkerThread();
			workerThread.setName("WorkerThread-"+i);
			workerThread.start();
		}	
	}
}