首页 > 解决方案 > 将 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);
}

标签: javacurlresttemplateapache-httpclient-4.x

解决方案


推荐阅读