首页 > 解决方案 > Apache HTTP 客户端下载 SOAP 响应的一部分

问题描述

我在使用 Java 从 SOAP 响应中下载文件时遇到问题。首先,我展示一个 SOAP-Ui 屏幕截图:

苏维

当我尝试使用 SOAP 客户端(Spring-WS 等)进行消费时,我只得到 XML 响应。当我发出 HttpRequest (cURL) 时,我得到:

------=_Part_156_25962124.1632296037066
Content-Type: text/xml; charset=utf-8

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><RetrieveChangeIAM (rest of soap response)

------=_Part_156_25962124.1632296037066
Content-Type: application/octet-stream
Content-Location: CH00672832.xlsx
Content-ID: cid:6123a4a00036402980f50f70
(binary-data)

(multiple ---=part)

我正在尝试使用 Apache Http Client 来拆分这些部分,但我找不到这样做的方法。有人能帮我吗?(我的实际代码下载了一个带有二进制数据的txt文件)

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
            HttpPost post = new HttpPost("http://endpoint/ws");
            post.addHeader("Authorization:", "Basic myauthbase64==");
            post.addHeader("Content-Type:", "text/xml; charset=utf-8");
            post.addHeader("Accept", "text/xml, multipart/related");

            StringEntity stringEntity = new StringEntity("<?xml version=\"1.0\" ?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header></SOAP-ENV:Header><S:Body><Rq>rq</Rq></S:Body></S:Envelope>");
            post.setEntity(stringEntity);

            final CloseableHttpResponse response = httpClient.execute(post);
            final HttpEntity entity = response.getEntity();

            log.info("HttpEntity: {}", entity);
            log.info("CloseableHttpResponse: {}", response);

            //observe all headers by this
            final Header[] h = response.getAllHeaders();
            for (int i = 0; i < h.length; i++) {
                log.info("\tHeader:{} => {}", h[i].getName(), h[i].getValue());
            }

            File targetFile = new File("out2.txt");

            if (entity != null) {
                InputStream inputStream = entity.getContent();
                OutputStream outputStream = new FileOutputStream(targetFile);
                IOUtils.copy(inputStream, outputStream);
                outputStream.close();
            }


        } catch (Exception e) {
            log.error("ERROR...", e);
        }

标签: javasoapapache-httpclient-4.xapache-commons

解决方案


推荐阅读