开发者

Is there a way to not download body during Java HttpClient call when the resource is large?

I'm trying to pre开发者_如何转开发vent 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜