Standalone JAVA Class using Apache HTTP Client

Introduction

 
Sometimes 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 for that third party till their application gets up and able to do end to end testing. There are certainly other ways to overcome this problem, like a web application can be made, from where data can be posted on middleware URL using HTTP Post or Get method. But here I come up with some simple solution from which a simple a standalone java class can be used to test your middleware 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 the environment setup
  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.
    1. PostMethod post = new PostMethod("URL"); 
  5. Create HTTP client (org.apache.commons.httpclient.HttpClient) object.
    1. 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.
    1. String s = "abcdefghijklmn";  
    2. 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.
    1. try {  
    2.     int result = httpclient.executeMethod(post);  
    3.     System.out.println("Response status code: " + result);  
    4.     System.out.println("Response body: ");  
    5.     System.out.println(post.getResponseBodyAsString());  
    6. finally {  
    7.     post.releaseConnection();  
    8. } 
  8. You might face a certificate issue with some URLs while using this code. No need to worry, Following are the steps to overcome this problem.
     
    • Download certificate from a 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
  1. /* 
  2.  * Created on Aug 23, 2011 
  3.  * 
  4.  * TODO To change the template for this generated file go to 
  5.  * Window - Preferences - Java - Code Style - Code Templates 
  6.  */  
  7. package com.test;  
  8.   
  9. import java.io.BufferedReader;  
  10. import java.io.ByteArrayInputStream;  
  11. import java.io.File;  
  12. import java.io.FileInputStream;  
  13. import java.io.IOException;  
  14. import java.io.InputStreamReader;  
  15. import org.apache.commons.httpclient.DefaultMethodRetryHandler;  
  16. import org.apache.commons.httpclient.HttpClient;  
  17. import org.apache.commons.httpclient.methods.InputStreamRequestEntity;  
  18. import org.apache.commons.httpclient.methods.PostMethod;  
  19. import org.apache.commons.httpclient.params.HttpMethodParams;  
  20.   
  21. /** 
  22.  * @author  
  23.  * 
  24.  * TODO To change the template for this generated type comment go to 
  25.  * Window - Preferences - Java - Code Style - Code Templates 
  26.  */  
  27. public class MYHTTPClient {  
  28.     public static void main(String[] args) throws IOException {  
  29.         String s = "abcdefghijklmn";  
  30.         StringBuilder sb = new StringBuilder(s);  
  31.         System.out.println(s.toString());  
  32.         PostMethod post = new PostMethod("http://domain/requestedpage");  
  33.         HttpClient httpclient = new HttpClient();  
  34.         post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(s.getBytes()), s.length()));  
  35.         try {  
  36.             int result = httpclient.executeMethod(post);  
  37.             System.out.println("Response status code: " + result);  
  38.             System.out.println("Response body: ");  
  39.             System.out.println(post.getResponseBodyAsString());  
  40.         } finally {  
  41.             post.releaseConnection();  
  42.         }  
  43.     }  
  44. } 


Similar Articles