IOUtils.toString和EntityUtils.toString之间有什么区别。应该使用下面哪一个来读取响应实体?为什么?
1) CloseableHttpResponse response = httpclient.execute(httpget);
data= EntityUtils.toString(response.getEntity());
OR
2) CloseableHttpResponse response = httpclient.execute(httpget);
data = IOUtils.toString(response.getEntity().getContent(),
StandardCharsets.UTF_8);
答案 0 :(得分:1)
使用EntityUtils
,它直接与实体对象一起使用。在第二个示例中,response.getEntity().getContent()
返回InputStream,必须将其关闭(您的示例代码不会这样做)。 EntityUtils
自己处理这个问题。 EntityUtils
还会读取实体标头中内容的编码,如果内容实际使用不同的编码进行编码,则不需要传递UTF_8常量,这可能是错误的。