0

I want to retrieve the file sizes of multiple urls. As of now, this is what I'm doing:

for ( int i = 0; i < urls.length; i++ ) {
    url = urls[i];
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod( "HEAD" );
    fileSizes[i] = conn.getContentLength();
}

But I think this is inefficient as it goes back and forth (request then a response, and over again).

Now, is there a way that I can perform a batch request for all those urls, so that the response will contain the file sizes of all those urls (only one request and one response)?

Also, I'm an absolute beginner in HTTP connections thus I don't even know if the title of this post is correct.

Sid Go
  • 2,041
  • 3
  • 16
  • 29
  • 1
    In HTTP/1.1 is an option to [request multiple files](https://en.wikipedia.org/wiki/HTTP_pipelining) at once, however almost no webserver supports that feature. Instead give http/2 a try there you can request multiple files too, however not all web servers supports that too. – rekire Jan 17 '17 at 08:08
  • See also [HTTP pipelining request text example](http://stackoverflow.com/q/19619124/995926). – rekire Jan 17 '17 at 08:44
  • `want to retrieve the file sizes of multiple urls`??? You are retrieving the content length. – greenapps Jan 17 '17 at 09:50
  • content length and file size mean the same thing – Robin Green Aug 14 '20 at 10:06

1 Answers1

0

You can use AsyncTask to run these queries in parallel. The results may then come back out of order so you have to adapt your code to handle that.

Simon
  • 6,293
  • 2
  • 28
  • 34