首页 > 解决方案 > Apache HttpClient:来自 HttpPost 的空响应实体

问题描述

我正在尝试使用 Apache 的 HttpClient 4.5.13 通过 Java 中的 POST 请求发送文件,并期望返回一个转换后的文件。但是,我得到的来自 HttpResponse 的 HttpEntity 没有内容。我已经使用 PHP 测试了 web 服务,并且可以按预期工作,但是在这里我什么也没得到。

我在这里做错了什么?

在我的 Java 代码下面:

private static HttpResponse makeRequest(String uri, File file) {
        try {
            // Create the entity containing the MV file to send with the post request
            FileEntity fileEntity = new FileEntity(file);

            // Create the post request and add the entity
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(fileEntity);

            // Create an http client and send the post request
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpResponse httpResponse = httpClient.execute(httpPost);

            System.out.println("---Result: ");

            String res = EntityUtils.toString(httpResponse.getEntity());
            System.out.println(res);
//            System.out.println(new BasicResponseHandler().handleEntity(httpResponse.getEntity()));

            return httpResponse;

        } catch (Exception e) {
            // When the execution of the post request fails (HttpClient.execute(...))
            e.printStackTrace();
            return null;
        }
    }

此外,确实给出了预期结果的 PHP 代码:

$filename = realpath("D:/svenp/Documents/CSE/Y2/Q4/SP/mvf/000002_20210401_120000.mvf");
    echo $filename, PHP_EOL;

    $post = array('mvf'=> curl_file_create($filename));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,            "http://.../..."); // Actual uri left out just in case 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST,           1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,     $post); 
    $result = curl_exec ($ch);
    curl_close ($ch);
    echo $result;
    $saveresult = file_put_contents("D:/svenp/Documents/CSE/Y2/Q4/SP/mvf/testresults/test2.vlg", $result);
    echo $saveresult;

编辑:从控制台返回的标题:

15:35:06.357 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
15:35:06.357 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Tue, 01 Jun 2021 13:35:07 GMT[\r][\n]"
15:35:06.357 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: Apache/2.4.46 (Win64) PHP/7.4.16[\r][\n]"
15:35:06.357 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "X-Powered-By: PHP/7.4.16[\r][\n]"
15:35:06.357 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Length: 0[\r][\n]"
15:35:06.357 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Keep-Alive: timeout=5, max=100[\r][\n]"
15:35:06.357 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: Keep-Alive[\r][\n]"
15:35:06.358 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: text/html; charset=UTF-8[\r][\n]"
15:35:06.358 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"

标签: javaapache-httpclient-4.x

解决方案


我有同样的问题,缺少请求标头属性是根本原因。就我而言,它是Accept: */*

尝试在调试模式下运行 curl 并检查请求头属性。


推荐阅读