首页 > 解决方案 > 如何从 SOAP 端点接收 xml 响应?

问题描述

我的问题是我有一个端点 url,如果我把它放到浏览器中,我可以看到该网站。当我在邮递员程序中使用正确请求(xml)的这个端点时,我收到了 xml 响应。这就是我想要的。但我试图在我的 java 应用程序中收到相同的响应,但我收到的是网站(html)。我不确定出了什么问题

JAXBContext jaxbContext = JAXBContext.newInstance(Envelope.class);
            Marshaller jaxbMarshaler = jaxbContext.createMarshaller();
            jaxbMarshaler.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            Date now = new Date();
            SimpleDateFormat formater = new SimpleDateFormat("ddMMyyhhmmss");
            fileName = "EMCS_" + tNumber.trim() + "_" + formater.format(now) + "_" + obtDto.getTransactionIdHost()
                + ".in";

            jaxbMarshaler.marshal(envelope, new File("C:\\Temp\\" + fileName));
            url = new URL(urlString);

            connection = (HttpsURLConnection) url.openConnection();
            connection.setUseCaches(false);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
            connection.setRequestProperty("Accept", "*/*");
            connection.setRequestProperty("SOAPAction", "");
            connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setRequestProperty("Authorization",
                "Basic " + Base64.encode("USER:PASSWORD".getBytes()));
            connection.setRequestProperty("Content-Type", "text/xml");

            BufferedOutputStream dos = new BufferedOutputStream(connection.getOutputStream());
            File file = new File("C:\\Temp\\" + fileName);
            BufferedInputStream fileOutput = new BufferedInputStream(new FileInputStream(file));
            byte[] b = new byte[(int) file.length()];

            for (int i = 0; i < b.length; i++) {
                b[i] = (byte) fileOutput.read();
                System.out.printf((char) b[i] + "");
            }

            dos.write(b);
            dos.flush();
            dos.close();
            fileOutput.close();

            System.out.println("RESPONSE: " + connection.getResponseCode());
            BufferedOutputStream responseWebsite = new BufferedOutputStream(
                new FileOutputStream("C:\\Temp\\response.html"));

            InputStream in = url.openStream(); // IMPORTANT TO READ PROPERLY DATA
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder result2 = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                responseWebsite.write(line.getBytes());
                result2.append(line + "\n");
            }

            responseWebsite.close();

通过使用上面的代码,我发布请求并获得 HTML 响应而不是 XML。我做错了什么?

编辑 我编辑了帖子,因为我认为我需要给出更多解释。我的问题是,我收到的“阅读器”可变字节是 HTML 而不是 XML。我想要 XML。

编辑2

所以我不能给出回购的链接,因为不是我的私人项目。但是当我使用邮递员并向端点发送请求时

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:de.aeb.xnsg.emcs.bf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body><ns2:request>...

我用我收到的应用程序生成的正文

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:response>... 

这很好。邮递员的响应是我想要的纯 XML。

但是,例如,当我将此端点放到 chrome 上时,我正在接收网站,在那里我可以找到该服务的其他端点/wsdls,但语言不同(法语、德语等)。

编辑 3 已解决

我的问题是我指定了 Accept-Encoding

connection.setRequestProperty("Accept-Encoding", "gzip,deflate");

当我删除它时,我可以从流中读取正确的响应。

但我也能够通过使用 GZIPInputStream 阅读此消息,但不确定哪种解决方案更好。

标签: javaxmlsoaphttpurlconnection

解决方案


也许问题出在这一行new FileOutputStream("C:\\Temp\\response.html"));?您应该将扩展名更改为 response.xml


推荐阅读