简体   繁体   English

如何读取HTTP POST请求返回的XML?

[英]How to read XML returned by HTTP POST request?

NOT a duplicate of my other question. 不能重复我的其他问题。

I am sending a POST request like this: 我正在发送这样的POST请求:

        String urlParameters = "a=b&c=d";
        String request = "http://www.example.com/";

        URL url = new URL(request);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();      
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false); 
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
        connection.setRequestProperty("charset", "utf-8");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
        connection.setUseCaches(false);

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
        connection.disconnect();

How can I read the xml response returned from a HTTP POST request? 如何读取HTTP POST请求返回的xml响应? Particularly, I want to save the response file as a .xml file, and then read it. 特别是,我想将响应文件另存为.xml文件,然后读取它。 For my usual GET requests, I use this: 对于我通常的GET请求,我使用以下命令:

    SAXBuilder builder = new SAXBuilder();
    URL website = new URL(urlToParse);
    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream("request.xml");
    fos.getChannel().transferFrom(rbc, 0, 1 << 24);
    fos.close();
    // Do the work

Addendum : I'm using the following code and it works just fine. 附录 :我正在使用以下代码,它可以正常工作。 However, it neglects any spacing and new lines and treats the complete XML contents as a single line. 但是,它忽略了任何空格和新行,并将完整的XML内容视为单行。 How do I fix it? 我如何解决它?

    InputStream is = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb1 = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb1.append(line);
    }
    FileOutputStream f = new FileOutputStream("request.xml");
    f.write(sb1.toString().getBytes());
    f.close();
    br.close();

don't use Readers and readLine() with xml data. 不要将ReadersreadLine()与xml数据一起使用。 use InputStreams and byte[] s. 使用InputStreamsbyte[]

Thanks to Pangea, I modified his code and this now works: 感谢Pangea,我修改了他的代码,现在可以使用:

    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer t= transFactory.newTransformer();
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.INDENT,"yes");                
    Source input = new StreamSource(is);
    Result output = new StreamResult(new FileOutputStream("request.xml"));
    transFactory.newTransformer().transform(input, output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM