0

I am using Apache HTTP Client to get file stream from url on Android,I want to check header of the stream to determine whether I should load the entire file, to avoid meaningless cost of network resource.
For now, I use the following code the get the inStream and just read the first few bytes from it:

response = mClient.execute(method, localcontext);
final HttpEntity entity = response.getEntity();
if (entity != null) {
    InputStream instream = entity.getContent();
    byte[] ints = new byte[2];
    instream.read(ints);
}

I am wondering if the whole file will be loaded in the above procedure. Should I close the stream explicitly. How?
Thanks~

4

1 回答 1

0

更好的方法是使用HEAD 方法。它只返回 http 标头,没有实际的内容或正文。

示例(来自官方网站

 HeadMethod head = new HeadMethod("http://jakarta.apache.org");
        // execute the method and handle any error responses.
        ...
        // Retrieve all the headers.
        Header[] headers = head.getResponseHeaders();

获得标题后,您可以检查它们,然后相应地进行另一个 GET/POST 调用以实际下载内容。

于 2013-05-16T07:09:03.103 回答