簡體   English   中英

如何從SOAP端點接收xml響應?

[英]How receive xml response from SOAP endpoint?

我的問題是我有一個端點網址,如果我放到瀏覽器,我可以看到網站。 當我在postman程序中使用此端點並且請求正確(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。 我做錯了什么?

編輯我編輯的帖子因為我需要給出更多的解釋。 我的問題是,我收到的'reader'變量字節是HTML而不是XML。 我想要XML。

EDIT2

所以我不能給出回購原因的鏈接不是我的私人項目。 但是當我使用郵遞員並向端點發送請求時

 <?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來閱讀此消息,但不確定哪種解決方案更好。

也許問題在於這一行new FileOutputStream("C:\\\\Temp\\\\response.html")); ?並且您應該將擴展名更改為response.xml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM