Standalone JAVA Class using Apache HTTP Client


Introduction : Sometime we work on some middleware project and face the issue for testing that application because that middleware application is used by some third party. And we have to wait that third party till their application gets up and able to do end to end testing. There are certain other ways to overcome this problem, like a web application can be made, from where data can be post on middle ware URL using HTTP Post or Get method. But here I come up with some simple solution from which a simple standalone java class can be used to test your middle ware application using http client provided by Apache.


Note : It can access those URL or application only for which your network has access.

Tools required for environment set up :

  1. Eclipse IDE (Any version which you prefer, or you can use any other ide in which you are comfortable or you can do it in notepad also).
     
  2. Jdk 1.5.
     
  3. Following jars

    • commons-codec-1.3.jar
    • commons-httpclient-3.0
    • commons-logging-1.0.3

Step to create your client :

  1. Create java project in eclipse.
     
  2. Add the below jars in class path

       commons-codec-1.3.jar
       commons-httpclient-3.0
       commons-logging-1.0.3
     
  3. Now you are ready to create your standalone java class.
     
  4. Create the object of org.apache.commons.httpclient.methods.PostMethod (GetMethod can also be used but it depends on your requirement). In the constructor of PostMethod you have to the URL of application to which you are going to request. Below is the code snippet.

    PostMethod post = new PostMethod("URL");
     

  5. Create HTTP client (org.apache.commons.httpclient.HttpClient) object.

    HttpClient httpclient = new HttpClient();
     

  6. Now it's time to set the data that you need to send the application and the responsibility will be taken care by PostMethod object. Below is the code snippet to set your request entity.

    String s = "abcdefghijklmn";
    post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(s.getBytes()), s.length()));
     

  7. Now it's high time for HttpClient to execute the request. Make connection with the application server and provide you response using PostMethod object.

    try
    {
         int result = httpclient.executeMethod(post);
         System.out.println("Response status code: " + result);
         System.out.println("Response body: ");
         System.out.println(post.getResponseBodyAsString());
     }
     finally
     {
         post.releaseConnection();
      }
     
  8. You might face certificate issue with some URLs while using this code. No need to worry, Following are the steps to overcome this problem.

    • Download certificate from particular site.
    • Put it into your jre's security folder
    • Execute following command on your command prompt

keytool -import -trustcacerts -file /path/to/ca/certificatefile -alias CA_ALIAS -keystore $JAVA_HOME/jre/lib/security/cacerts

Consolidated Code :

/*
 * Created on Aug 23, 2011
 *
 *
TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.test;
 
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.DefaultMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams; 
 
/**
 *
@author
 *
 *
TODO To change the template for this generated type comment go to
 * Window
- Preferences - Java - Code Style - Code Templates
 */
public class MYHTTPClient
{
      
public static void main(String[] args) throws IOException
      {
            String s =
"abcdefghijklmn";
            StringBuilder sb =
new StringBuilder(s);
            System.
out.println(s.toString());
            PostMethod post =
new PostMethod("http://domain/requestedpage");
            HttpClient httpclient =
new HttpClient();
            post.setRequestEntity(
new InputStreamRequestEntity(new  ByteArrayInputStream(s.getBytes()), s.length()));
       
     try
            {
           
    int result = httpclient.executeMethod(post);
                System.
out.println("Response status code: " + result);
                System.
out.println("Response body: ");
                System.
out.println(post.getResponseBodyAsString());
            }
           
finally
            {
                post.releaseConnection();
            }
       }
}

Up Next
    Ebook Download
    View all
    Learn
    View all