java - 将 RestTemplate / Apache HttpClient 转换为 cURL
问题描述
我需要将 restTemplate 转换为 cURL。我有这个代码:
public ResponseEntity<String> getProtocol(byte[] dataByteArray, byte[] signatureByteArray) throws IOException {
HttpHeaders headers = createHttpHeaders();
MultiValueMap<String, Object> multipartContent = createMultipartContent(
dataByteArray,
signatureByteArray
);
HttpEntity<MultiValueMap<String, Object>> entity = new org.springframework.http.HttpEntity<>(multipartContent, headers);
return restTemplate.postForEntity(
validationProperties.getSignatureValidationPath(),
entity,
String.class
);
}
private HttpHeaders createHttpHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/form-data; charset=UTF-8");
headers.setAccept(Lists.newArrayList(MediaType.ALL));
return headers;
}
private MultiValueMap<String, Object> createMultipartContent(byte[] data, byte[] signature) {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add(KEY_MULTIPART_DATA, composeHttpEntity(data, KEY_MULTIPART_DATA));
body.add(KEY_MULTIPART_SIGNATURE, composeHttpEntity(signature, KEY_MULTIPART_SIGNATURE));
return body;
}
private HttpEntity<byte[]> composeHttpEntity(byte[] data, String name) {
ContentDisposition contentDisposition = ContentDisposition
.builder("form-data")
.name(name)
.filename(name)
.build();
LinkedMultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
fileMap.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
fileMap.add("Content-Transfer-Encoding", "binary");
return new HttpEntity<>(data, fileMap);
}
我想从中编写 cURL 请求。怎么做?
我有一些标题问题。另外,请说明我如何传输文件 - 从代码来看,它们是作为二进制代码传输的。
我也有代码的 Apache Http 选项:
public ResponseEntity<String> getProtocol(public ResponseEntity<String> getProtocol(byte[] dataByteArray, byte[] signatureByteArray) throws IOException {) throws FileServiceException, IOException {
final org.apache.http.HttpEntity multipartEntity = MultipartEntityBuilder
.create()
.addPart(KEY_MULTIPART_DATA, new InputStreamBody(dataByteArray, KEY_MULTIPART_DATA))
.addPart(KEY_MULTIPART_SIGNATURE, new InputStreamBody(signatureByteArray, KEY_MULTIPART_SIGNATURE))
.setCharset(Charsets.UTF_8)
.build();
String url = validationProperties.getSignatureValidationPath();
final HttpPost post = new HttpPost(url);
post.setEntity(multipartEntity);
post.setHeader("Content-Type", "multipart/form-data");
CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(post);
String s = EntityUtils.toString(response.getEntity());
return ResponseEntity.ok(s);
}
解决方案
推荐阅读
- rust - 为什么 1 == &2 不能编译?
- xml - 从 XML 中读出所有带有路径的属性
- javascript - 什么是异步 void 的 typedef
- python - 如何在 Odoo v8 中存储旧 API 中声明的计算字段?
- mongodb - 使用可变流星 mongo 更新字段
- windows-installer - 挂载 ISO 时触发 setup.exe
- wordpress - 测试 WordPress 自定义帖子类型变量
- reactjs - Material-ui 按钮中的字体真棒图标
- r - 有什么方法可以从 R 中的多层栅格网格中排除某些时期的天气数据?
- java - 如何在 IntelliJ IDEA 中将文件从源目录移动到具有相同路径的资源目录?