As I agreed in the previous blog post
for the migration guide of Commons HttpClient 3.x to HttpComponents HttpClient
4.x. Even I also took time to migrate, let me put my effort here so that
someone can reap from it.
HttpComponents
HttpClient is a HTTP/1.1 compliant HTTP agent implementation based on
HttpCore. It also provides reusable components for client-side authentication,
HTTP state management, and HTTP connection management. HttpComponents Client is
a successor of and replacement for Commons HttpClient 3.x. Users of Commons
HttpClient are strongly encouraged to upgrade.
Below table is the quick reference
for the migration.
Commons HttpClient 3.x
|
HttpComponents HttpClient 4.x
|
|
import
|
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.
MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.
UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import
org.apache.commons.httpclient.methods.DeleteMethod;
import
org.apache.commons.httpclient.methods.GetMethod;
import
org.apache.commons.httpclient.methods.PostMethod;
|
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import
org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import
org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.util.EntityUtils;
|
HttpClient
|
HttpClient client = new HttpClient(connectionManager);
|
DefaultHttpClient client = new DefaultHttpClient();
|
proxy
configuration
|
client.getHostConfiguration().setProxy( proxyHost, proxyPort);
client.getState().setAuthenticationPreemptive( true);
Credentials cred = new
UsernamePasswordCredentials( proxyUser, proxyPassword);
client.getState().setProxyCredentials( AuthScope.ANY_HOST,
proxyHost, cred)
|
client.getCredentialsProvider().
setCredentials(
new
AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials( proxyUser,
proxyPassword));
HttpHost proxy = new
HttpHost(proxyHost proxyPort);
client.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY,
proxy);
|
POST
|
PostMethod post = new PostMethod();
post.setPath(url.getFile());
|
HttpPost post = new HttpPost(url.getFile());
|
POST
Header
|
post.setRequestHeader(key, (String) headers.get(key));
|
post.addHeader(key, (String) headers.get(key));
|
POST
Body
|
post.setRequestBody(message);
|
StringEntity
messageEntity = new StringEntity( message,
ContentType.create("text/plain",
"UTF-8"));
post.setEntity(messageEntity);
|
Execute
POSt
|
client.executeMethod(post);
|
HttpHost targetHost
= new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
HttpResponse httpResponse = client.execute(targetHost, post);
|
Response
Header
|
Header[] rspHeaders = post.getResponseHeaders();
|
Header[] rspHeaders = httpResponse.getAllHeaders();
|
Response
Body
|
String responseMsg = post.getResponseBodyAsString()
|
StringBuffer buffer
= new StringBuffer();
BufferedReader reader = new
BufferedReader(new
InputStreamReader(httpResponse.getEntity().getContent()));
String dataLine = null;
while((dataLine = reader.readLine()) != null){
buffer.append(dataLine);
}
String responseMsg = buffer.toString();
|
GET
|
GetMethod getMethod =
new GetMethod();
getMethod.setPath(url.getFile());
|
HttpGet httpGet = new
HttpGet(url.getFile());
|
GET
Header
|
getMethod.setRequestHeader(key,
value);
|
httpGet.addHeader(key,
value);
|
Execute
GET
|
client.executeMethod(getMethod);
|
HttpHost targetHost
= new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
HttpResponse
httpResponse = client.execute( targetHost, httpGet);
|