当资源很大时,有没有办法在 Java HttpClient 调用期间不下载正文?

发布于 2024-10-05 05:48:48 字数 358 浏览 0 评论 0原文

我试图阻止 org.apache.http.client.HttpClient (链接文本),当资源超过 10 MB 时下载正文。

我在这里搜索后所做的以及其他人所做的就是进行 HEAD 调用,然后检查 Content-Length 标头,如果它是 < 10MB,执行 GET 调用,最后对响应正文执行一些操作。

对此是否有更好的解决方案?我想避免额外的电话。

谢谢。

I'm trying to prevent org.apache.http.client.HttpClient (link text) from downloading the body when the resource let's say over 10 MB.

What I have done after searching here, and others is do a HEAD call, then check the Content-Length header, and if it's < 10MB, do a GET call, finally do something with the response body.

Would there be a better solution to this? I want to avoid an extra call.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚然 2024-10-12 05:48:48

您可以随时进行常规调用并检查长度,如果长度 > 10MB 然后就中断并取消下载。但问题是,在您取消之前,套接字仍然会下载尽可能多的内容。我认为进行 HEAD 调用是最好的方法。

You can always do a regular call and check the length, and if the length > 10MB then just break and cancel the download. But the problem is that socket will still download as much as possible before you cancel. I think doing a HEAD call is the best method.

鼻尖触碰 2024-10-12 05:48:48

最好的方法肯定是执行 HTTP HEAD,因为 HEAD 请求文件或资源的标头信息(如 RFC1945,第 8.2 节)。

从 HEAD 调用的响应中,检索 Content-Length 消息标头并查看大小是否小于或大于 10MB。

执行 GET 将返回完整的实体主体。

Apache HttpClient(版本 3)支持 HTTP HEAD。

The Best way is definitely to do the HTTP HEAD since HEAD request for only header information on file or resource (as stipulated on RFC1945, Section 8.2).

From the response of the HEAD call, retrieve the Content-Length message header and see if the size is less than or greater than 10MB.

Doing a GET will result in returning the full entity body.

Apache HttpClient (Version 3) supports HTTP HEAD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文