当资源很大时,有没有办法在 Java HttpClient 调用期间不下载正文?
我试图阻止 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以随时进行常规调用并检查长度,如果长度 > 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.
最好的方法肯定是执行 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.