无法从HttpClient获得完整的HTTP响应

杰森·沙皮罗(Jason Shapiro)

我似乎没有从httpclient获得整个json响应。我碰到了我正在本地运行的api,如下所示:

curl -i -X POST http://localhost:8098/<api location> -F "files=@<filename>"

我的回答如下:

{"data":[<bunch of json>]

但是,当我尝试使用httpclients发布完全相同的文件时,得到以下响应:

{"data":[]}

我究竟做错了什么?这是我的Java代码。谢谢!

public CloseableHttpResponse submit (File file) throws IOException {
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost post = new HttpPost(API_LOCATION + API_BASE);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addBinaryBody("ISO xml file", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
    HttpEntity multipartEntity = builder.build();
    post.setEntity(multipartEntity);

    CloseableHttpResponse response = client.execute(post);
    System.out.println("response: " + IOUtils.toString(response.getEntity().getContent(),"UTF-8"));
    client.close();
    return response;
}
杰森·沙皮罗(Jason Shapiro)

正如Andreas和Danilo在评论中提到的那样:

在curl中,您将命名字段文件,而在Java中,则将其命名为ISO xml文件。由于服务器仅查找文件,因此看不到任何内容,并且不响应。-安德烈亚斯

参数名称呢?在curl上,您使用“文件”,在http客户端上,您使用“ ISO xml文件”。尝试将其更改为“文件”。-达尼洛

我需要将“ ISO xml文件”更改为“文件”,并且可以正常工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章