Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how can I make an HTTP HEAD request?

I would like to make a simple HTTP HEAD request, without keep-alive.

How can I do that in Android?

like image 925
Daniele B Avatar asked Mar 20 '14 18:03

Daniele B


People also ask

How do I make a HTTP HEAD request?

To make a HEAD request with Curl, you need to use the -I or --head command-line parameter. The -I command-line parameter tells Curl to send an HTTP HEAD request to receive only HTTP headers. The HEAD request is very similar to a GET request, except that the server only returns HTTP headers without a response body.

Is head a HTTP request?

HEAD is a request method supported by HTTP used by the World Wide Web. The HEAD method asks for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

How do you use HTTP HEAD method?

The HTTP HEAD method requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file.

Is HTTP head faster than GET?

The HEAD method is used to ask only for information about a document, not for the document itself. HEAD is much faster than GET, as a much smaller amount of data is transferred. It's often used by clients who use caching, to see if the document has changed since it was last accessed.


1 Answers

using HttpClient:

As njzk2 suggested, with HttpClient() it's very straightforward:

HttpResponse response = new HttpClient().execute(new HttpHead(myUrl));

However there is a problem with not being able to close the connection. Usually on the HttpClient, you would get the entity using:

HttpEntity entity = response.getEntity();

and then you would get the input stream from the entity

InputStream instream = entity.getContent();
...
instream.close();

and by closing the input stream, the connection would close. However, in the case of a HEAD request, the entity appears to be null (possibly because HEAD requests don't return the body in the response), so the input stream cannot be fetched and closed and the connection doesn't close either.

In the last edit to his answer, njzk2 is suggesting to use AndroidHttpClient, which is a more recent implementation (API 8) of HttpClient and it actually has a close() method. I haven't used it but I guess it will work fine. However, as the Android development team suggests, the HttpUrlConnection should be the preferred Android client to use.

using HttpUrlConnection:

Actually it seems quite easy to make HEAD requests using HttpUrlConnection and make sure that the connection closes:

    HttpURLConnection urlConnection = null;
    System.setProperty("http.keepAlive", "false");
    try {
        URL url = new URL(stringUrl);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("HEAD");
        urlConnection.getInputStream().close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
like image 53
Daniele B Avatar answered Oct 04 '22 17:10

Daniele B