Blog Archive

Java XML HTTP post example using org.apache.commons.httpclient

HttpClient is a client side HTTP transport library. HttpClient's purpose is to transmit and receive HTTP messages. You can also use the sample code to http post files other than XML format.

HttpClient provides several classes that can be used to efficiently stream out content though HTTP connections. Instances of those classes can be associated with entity enclosing requests such as POST and PUT in order to enclose entity content into outgoing HTTP requests. HttpClient provides several classes for most common data containers such as string, byte array, input stream, and file: StringEntity, ByteArrayEntity, InputStreamEntity, and FileEntity.
File file = new File("somefile.txt");
FileEntity entity = new FileEntity(file, "text/plain; charset=\"UTF-8\"");
HttpPost httppost = new HttpPost("http://localhost/");

You need the following jars for the sample java code to work
  • httpcore-4.1.jar
  • httpclient-4.1.1.jar
  • commons-logging-1.1.1.jar

Click here to download HttpClient from Apache Commons
package com.as400samplecode;

import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpXMLDataPost {

    static Properties props;

    public static void main(String[] args)
        String fileName = "";
        String propertiesFileName = "";

        if (args.length < 2)
            HttpXMLDataPost httpXMLDataPost = new HttpXMLDataPost();
            System.err.println("Usage: java "+ httpXMLDataPost.getClass().getName()+
            " XML_File_Name Properties_File_Name");

        fileName = args[0].trim();
        propertiesFileName = args[1].trim();
        boolean success = XMLDataPost(fileName, propertiesFileName);

    private static boolean  XMLDataPost(String fileName, String propertiesFileName){

        boolean success = false;
        HttpClient httpclient = new DefaultHttpClient();

        try {

            props = new Properties();
            props.load(new FileInputStream("properties/" + propertiesFileName.trim()));

            HttpPost httpPost = new HttpPost(props.getProperty("post_url").trim() );

            File file = new File(fileName.trim());

            InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);

            System.out.println("Executing request " + httpPost.getRequestLine());
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

            if(response.getStatusLine().getStatusCode() == 200){
                success = true;
            if (resEntity != null) {
                System.out.println("Response content length: " + resEntity.getContentLength());
                System.out.println("Chunked?: " + resEntity.isChunked());
        catch (Exception e) {
        finally {

        return success;



Sample properties file for the Http Post

#Properties File for XML data http post

#URL for submitting XML data