POST xml data using java

Viewed 41039

I have used the following java code to POST xml data to a remote url and get the response. Here, I am using an xml file as the input. What I need is to pass the xml as a string not a file... is there anyway I can do this? Can someone help me? Thanks a lot!

Java code

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

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

public class xmlToString {

public static void main(String[] args) {
    String strURL = "https://simulator.expediaquickconnect.com/connect/ar";
    String strXMLFilename = "xmlfile.xml";
    File input = new File(strXMLFilename);
    PostMethod post = new PostMethod(strURL);
    try {
        post.setRequestEntity(new InputStreamRequestEntity(
                new FileInputStream(input), input.length()));
        post.setRequestHeader("Content-type",
                "text/xml; charset=ISO-8859-1");
        HttpClient httpclient = new HttpClient();

        int result = httpclient.executeMethod(post);
        System.out.println("Response status code: " + result);
        System.out.println("Response body: ");
        System.out.println(post.getResponseBodyAsString());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        post.releaseConnection();
    }
}

   }

UPDATE: I need to pass XML as a string and remove involving xml file...

3 Answers
Related