从 HttpClient 3 转换为 4
我已经设法对除以下内容之外的所有内容进行更改:
HttpClient client;
HttpPost method;
client = new DefaultHttpClient();
method = new HttpPost(url);
InputStream rstream;
try {
rstream = method.getResponseBodyAsStream();
} catch (IOException e) {
return BadSpot(e.getMessage());
}
我不确定应该用什么替换 getResponseBodyAsStream() 。
I've managed to make changes to everything but the following:
HttpClient client;
HttpPost method;
client = new DefaultHttpClient();
method = new HttpPost(url);
InputStream rstream;
try {
rstream = method.getResponseBodyAsStream();
} catch (IOException e) {
return BadSpot(e.getMessage());
}
What I'm not sure of is what I should replace getResponseBodyAsStream() with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
上面应该做你所要求的。
above should do what you are asking.
HttpResponse.getEntity (),后跟 HttpEntity.getContent()
HttpResponse.getEntity(), followed by HttpEntity.getContent()
util 类有一些有用的方法:
apache 的示例中也有一些有用的内容网站
The util class has some helpful methods:
There is also some useful stuff in the examples at apache's website
使用 EntityUtils 并在使用实体之前检查返回的实体是否为 null:
注意:此处的 InputStream 可以为 null,最重要的是必须确保在实际关闭响应/释放连接之前它已被消耗。
Use
EntityUtils
and check the returned entity to benot null
before consuming the entity:NOTE: the InputStream here can be null and most of all you have to ensure that it's consumed before you actually close the response/release the connection.