REST client using HttpClient 4 api

This article will assist in creating a Client for invoking a Rest service.. we are using HttpClient 4.3.5 jar for this.

 

We need to download the following jars for this:

httpclient 4.3.5.jar

commons-httpcore 4.3.2.jar

httpclient-cache 4.3.5.jar

commons-logging 1.1.3.jar

 

You can directly download these jars from apache site or use maven to download the dependcies for you.

We are doing a post call using HttpPost class.

 

Create HttpPost object from the service url.

 

HttpPost post = new HttpPost(serviceURL);

 

Read the JSON request from the json file. If you have a json object, you can directly use it.

 

JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("C:\temp\TestRequest.json"));

JSONObject jsonRequest = (JSONObject) obj;

Add the request to the HttpPost object

 

StringEntity input = new StringEntity(jsonRequest.toString());

input.setContentType(“application/json”);

post.setEntity(input);
Call the REST service

 

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);

Read the response.. you can convert it to JSON if needed

 

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}

If you are using SSL and need to use the server provided certificate for establishing the connection, use the keytool utility to add the certificate to the keystore

keytool -import -alias <aliasname> -file <certificate file name with .cer> -keystore <keystore name>.jks

 

In the code for rest client, add the following code :

 

final String KEY_STORE_PATH = System.getProperty("javax.net.ssl.keyStore");
final String KEY_STORE_PASSWORD = System.getProperty("javax.net.ssl.keyStorePassword");
final String TRUST_STORE_PATH = System.getProperty("javax.net.ssl.trustStore");
final String TRUST_STORE_PASSWORD = System.getProperty("javax.net.ssl.trustStorePassword");

//load the keystore

KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream keystoreInput = new FileInputStream(KEY_STORE_PATH);
keystore.load(keystoreInput, KEY_STORE_PASSWORD.toCharArray());

//load the truststore

KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream truststoreInput = new FileInputStream(TRUST_STORE_PATH);
truststore.load(truststoreInput, TRUST_STORE_PASSWORD.toCharArray());

//create SSLSocketFactory with the keystore & truststore.. register with the client instance

DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry supportedSchemes = client.getConnectionManager().getSchemeRegistry();
SSLSocketFactory lSchemeSocketFactory = new SSLSocketFactory(keystore, KEY_STORE_PASSWORD, truststore);
supportedSchemes.register(new Scheme("https", 443, lSchemeSocketFactory));

© 2015, https:. All rights reserved. On republishing this post, you must provide link to original post

One comment

  1. […] client using HttpClient 3 api    REST client using HttpClient 4 api   […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: