java - 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]"
解决方案
我有同样的问题,缺少请求标头属性是根本原因。就我而言,它是Accept: */*
尝试在调试模式下运行 curl 并检查请求头属性。
推荐阅读
- sql-server - T-SQL: alter sequence number current_value without changing start_value
- html - Hamburger nav not clickable - already checked scripts and CDN
- c# - How to get self name
- javascript - 使用 getJSON 时如何使用变量作为 JSON 文件名
- r - 具有不同 x 刻度的双 Y 轴基础 R 图
- android - how to deal with 2 FragmentTransaction
- cordova - 使用带有--release标签的离子构建时任务':app:mergeReleaseResources'的执行失败
- flutter - 作为 api 请求的结果,如何将变量获取到文本字段中?
- css - 有没有办法对 CSS 网格中的项目流进行蛇行/排队?
- reactjs - 带有电子邮件字段的组件,当用户单击按钮并且我需要验证电子邮件地址时,该字段将回传到服务器