Java Apache HttpClient:HttpPost中的空响应实体
我试图使用Apache的HttpClient 4.5.13,通过java中的POST请求发送一个文件,并期望返回一个转换后的文件。但是,我得到的HttpResponse中的HttpEntity没有内容。我已经使用PHP测试了Web服务,效果如预期,但在这里我没有得到任何结果 我做错了什么 下面是我的Java代码:Java Apache HttpClient:HttpPost中的空响应实体,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我试图使用Apache的HttpClient 4.5.13,通过java中的POST请求发送一个文件,并期望返回一个转换后的文件。但是,我得到的HttpResponse中的HttpEntity没有内容。我已经使用PHP测试了Web服务,效果如预期,但在这里我没有得到任何结果 我做错了什么 下面是我的Java代码: private static HttpResponse makeRequest(String uri, File file) { try { /
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]"
15:35:06.357[main]调试org.apache.http.wire-http-outing-0