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/action.do");
httppost.setEntity(entity);

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.io.File;
import java.io.FileInputStream;
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");
            System.exit(1);
        }

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

    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);
            reqEntity.setContentType("application/xml");
            reqEntity.setChunked(true);
           
            httpPost.setEntity(reqEntity);

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

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

        return success;

    }

}

Sample properties file for the Http Post

#Properties File for XML data http post

#URL for submitting XML data
post_url=http://as400.samplecode.blogspot.com/sendorder?import=true

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.