Here's some snippet code how we implemented HttpClient using the
MultiThreadedHttpConnectionManager...
import java.io.*;
import java.util.*;
import java.text.*;
import java.security.Security;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.*;
import org.apache.commons.httpclient.protocol.*;
public class MyHttpclientExample{
public String protocol = http;
public String host = localhost;
public int port = 80;
public String httpAuthRealm = null;
public String httpAuthUsername = null;
public String httpAuthPassword = null;
public String trustStore= null;
public String trustStoreType= jks;
public String trustStorePassword= null;
public String trustStoreProvider= SUN;
public String trustStoreAlgorithm = SunX509;
public String keyStore = null;
public String keyStoreType = jks;
public String keyStorePassword = null;
public String keyStoreProvider = SUN;
public String keyStoreAlgorithm = SunX509;
public String path = null;
public ByteArrayOutputStream responseAsStream = null;
public String saveAsFilename= null;
private Hashtable parameters= null;
private Hashtable cookies = null;
public HttpClient httpClient = null;
Protocol myProtocol= null;
AuthSSLProtocolSocketFactory authSSLProtocolSocketFactory = null;
Cookie cookie = null;
NameValuePair[] nameValuePairs = null;
String s1 = null;
String s2 = null;
String s3 = null;
int i1 = 0;
int i2 = 0;
int i3 = 0;
//
String url= null;
/*
HTTPConnection httpConnection = null;
HTTPResponse resp = null;
*/
BufferedReader reader = null;
StringBuffer returnData = null;
public void resetParameters()throws Throwable{
parameters = null;
}
public void addParameter(String name, String value)throws Throwable{
if (parameters == null){
parameters = new Hashtable();
}
parameters.put(name,value);
}
public void resetCookies()throws Throwable{
cookies = null;
}
public void addCookie(String name, String value)throws Throwable{
if (cookies == null){
cookies = new Hashtable();
}
cookies.put(name,value);
}
public void connect()throws Throwable{
httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
if (protocol.equals(https)){
authSSLProtocolSocketFactory = new AuthSSLProtocolSocketFactory();
authSSLProtocolSocketFactory.keyStoreName = keyStore;
authSSLProtocolSocketFactory.keyStorePassword = keyStorePassword;
authSSLProtocolSocketFactory.trustStoreName = trustStore;
authSSLProtocolSocketFactory.trustStorePassword = trustStorePassword;
myProtocol = new Protocol(protocol, authSSLProtocolSocketFactory,
port);
httpClient.getHostConfiguration().setHost(host, port, myProtocol);
}else{
httpClient.getHostConfiguration().setHost(host, port, protocol);
}
HttpState initialState = new HttpState();
if (cookies != null){
for (Enumeration e = cookies.keys(); e.hasMoreElements();){
s1 = (String) e.nextElement();
cookie = new Cookie(host, s1, (String) cookies.get(s1), /, -1,
false);
initialState.addCookie(cookie);
}
}
if (httpAuthUsername != null){
httpClient.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new
UsernamePasswordCredentials(httpAuthUsername, httpAuthPassword);
initialState.setCredentials(new AuthScope(host, port, httpAuthRealm),
defaultcreds);
}
httpClient.setState(initialState);
}
public void get()throws Throwable{
get(path);
}
public void get(String path)throws Throwable{
if (parameters == null){
nameValuePairs = null;
}else{
nameValuePairs = new NameValuePair[parameters.size()];
i1 = -1;
for (Enumeration e = parameters.keys(); e.hasMoreElements();){
i1 ++;
s1 = (String) e.nextElement();
nameValuePairs[i1] = new NameValuePair(s1,(String)
parameters.get(s1));
}
}
get(path,nameValuePairs);
}
public int get(String path, NameValuePair[] nameValuePairs)throws
Throwable{
int i = 0;
url = / + path;
GetMethod method = new GetMethod(url);
method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
if (nameValuePairs != null){
method.setQueryString(nameValuePairs);
}
// Execute the method.
responseAsStream = new ByteArrayOutputStream();
int statusCode = httpClient.executeMethod(method);
if (statusCode == HttpStatus.SC_OK) {
byte[] buffer