在 HttpClient 4 中读取和终止流 [英] Reading and terminating stream in HttpClient 4

查看:23
本文介绍了在 HttpClient 4 中读取和终止流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读只需要前 5% 的大文件,我可以使用 HttpClient 4 执行以下操作吗?

I'm reading large documents from which I only need top 5%, can I do the following with HttpClient 4?

  1. 请求页面(获取或发布)
  2. 以流的形式读取响应
  3. 即时"将其输入到基于 SAX 的 HTML 解析器中
  4. 当检测到某个 HTML 标签时 - 终止流

请注意 HttpClient v. 4 是必需的 - 我不能使用 v. 3

Please note that HttpClient v. 4 is required - I cannot use v. 3

推荐答案

感谢来自 HttpClient 邮件列表的 Ken,这是答案

Thanks to Ken from HttpClient mail list here's the answer

使用 HttpEntity#getContent()方法,它返回一个
java.io.InputStream,并将其传递给基于 SAX 的 HTML 解析器.

Use the HttpEntity#getContent() method, which returns an
java.io.InputStream, and pass that to your SAX-based HTML parser.

http://hc.apache.org/httpcomponents-client/tutorial/html/fundamentals.html#d4e122

当你看到你需要的标签时,通过调用终止请求HttpUriRequest#abort() 方法.

When you see the tag you need, terminate the request via invoking the HttpUriRequest#abort() method.

http://hc.apache.org/httpcomponents-client/tutorial/html/fundamentals.html#d4e285

这篇关于在 HttpClient 4 中读取和终止流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆