9

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...

0

3 Answers 3

8

The setRequestEntity method on org.apache.commons.httpclient.methods.PostMethod has an overloaded version that accepts StringRequestEntity as argument. You should use this if you wish to pass in your data as a string (as opposed to an input stream). So your code would look something like this:

String xml = "whatever.your.xml.is.here";
PostMethod post = new PostMethod(strURL);     
try {
    StringRequestEntity requestEntity = new StringRequestEntity(xml);
    post.setRequestEntity(requestEntity);
....

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

this dint work for me. please help this question stackoverflow.com/questions/32884587/…
How can I elegantly create XML key value pairs instead of using pure Strings?
1

You can convert the XML to String from this method

public String convertXMLFileToString(String fileName) 
{ 
   try{ 
       DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
       InputStream inputStream = new FileInputStream(new File(fileName)); 
       org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); 
       StringWriter stw = new StringWriter(); 
       Transformer serializer = TransformerFactory.newInstance().newTransformer(); 
       serializer.transform(new DOMSource(doc), new StreamResult(stw)); 
       return stw.toString(); 
   } 
   catch (Exception e) { 
       e.printStackTrace(); 
   } 
   return null; 
}

Add you can pass this string as a parameter on PostMethod like this.

PostMethod post = new PostMethod(strURL);
post.addParamter("paramName", convertXMLFileToString(strXMLFilename ) );

The whole XML will be transmitted to the client in a queryString.

1 Comment

Hi, Thanks a lot for your answer, I don't need to involve a xml file at all instead I need to pass xml as a string... is there a way to do that?
0

To get your XML file contents as a String use (add catch-block for IOException)

StringBuilder bld = new StringBuilder();
FileReader fileReader = new FileReader(input);
BufferedReader reader = new BufferedReader(fileReader);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    bld.append(line);
}
String xml = bld.toString();

The better way is to use Java Web Services JAX-WS or Java Restful Web Services JAX-RS.

1 Comment

Hi, Thanks a lot for your answer, I don't need to involve a xml file at all instead I need to pass xml as a string... is there a way to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.