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
- 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).
- Jdk 1.5.
- Following jars
- commons-codec-1.3.jar
- commons-httpclient-3.0
- commons-logging-1.0.3
Step to create your client
- Create java project in eclipse.
- Add the below jars in class path
commons-codec-1.3.jar
commons-httpclient-3.0
commons-logging-1.0.3
- Now you are ready to create your
standalone java class.
- 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");
- Create HTTP client (org.apache.commons.httpclient.HttpClient)
object.
- HttpClient httpclient = new HttpClient();
- 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()));
- 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();
- }
- 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
-
-
-
-
-
-
- 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;
-
-
-
-
-
-
-
- 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();
- }
- }
- }