java - 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);
}
解决方案
推荐阅读
- python - 截图 element 的所有子元素
- java - 当具有多个路径段时,引导组件未在 Thymeleaf 模板中呈现
- python - 是否有更有效或更简洁的方法来根据索引列表划分 df?
- java - 如何通过spring cloud stream binder kafka流依赖使用协议缓冲区(protobuf)使用来自kafka主题的消息?
- zsh - 我在 zparseopts 中发现了一个错误还是我做错了
- awk - 使用多个多字符分隔符时替换单个字段中的字符
- laravel - 通知,获取“喜欢”推文的用户
- oracle - 计算工资期结束时的年龄
- arrays - 如何使用 ndarray 从向量向量创建二维数组?
- html - 从 Angular 的文本框中添加/删除值