Sunday, February 6, 2011

Simple Post using Java

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.codec.DecoderException;


String url = args[0];
        String strXMLFilename = args[1];
        System.out.println("Sent File\n"+getContents(strXMLFilename).toString());
        File input = new File(strXMLFilename);
        PostMethod post = new PostMethod(url);
        try{
        post.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(input), input.length()));
        }catch(IOException e){
            e.printStackTrace();
        }
        post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
        HttpClient httpclient = new HttpClient();
        try{
            int result = httpclient.executeMethod(post);
               System.out.println("Response status code: " + result);
            System.out.println("Response body: "+post.getResponseBodyAsString());
            System.out.println("Response body: "+post.getStatusText());
        } catch(Exception e){
            e.printStackTrace();
        }finally {
            post.releaseConnection();
        }

No comments:

Post a Comment