使用Java POST发送XML数据

9
我使用以下Java代码将XML数据POST到远程URL并获取响应。在这里,我使用一个XML文件作为输入。我需要的是将XML作为字符串传递而不是文件...有没有办法可以做到这一点?有人能帮帮我吗?非常感谢! Java代码:
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();
    }
}

   }

更新: 我需要将XML作为字符串传递,并删除涉及XML文件的部分...

3个回答

8

org.apache.commons.httpclient.methods.PostMethod的setRequestEntity方法有一个重载版本,接受StringRequestEntity作为参数。如果您希望将数据作为字符串传递(而不是输入流),则应使用此方法。因此,您的代码应该类似于:

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

希望有所帮助。

这对我没用。请帮忙回答这个问题:http://stackoverflow.com/questions/32884587/how-to-sent-xml-file-endpoint-url-in-restful-web-service/32884767#32884767 - Thilina Sampath
我该如何优雅地创建XML键值对,而不是使用纯字符串? - therealprashant

1
您可以通过此方法将XML转换为字符串。
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; 
}

你可以将这个字符串作为参数传递给PostMethod,像这样。

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

整个 XML 将以 queryString 的形式传输到客户端。

你好,非常感谢您的回答。我根本不需要涉及到 XML 文件,而是需要将 XML 作为字符串传递...有什么办法可以做到这一点吗? - Pavithra Gunasekara

0

要将XML文件内容作为字符串获取,请使用以下代码(添加IOException的catch-block)

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();

更好的方法是使用Java Web Services JAX-WS或Java Restful Web Services JAX-RS。

你好,非常感谢您的回答。我根本不需要涉及到 XML 文件,而是需要将 XML 作为字符串传递...有什么办法可以做到这一点吗? - Pavithra Gunasekara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接