1

I am opening an HTTP connection to a URL that I am generating within my program:

String url = INPUT_URL;
HttpURLConnection connection;
while (true)
{
    connection = (HttpURLConnection)new URL(url).openConnection();
    connection.setInstanceFollowRedirects(true);
    switch (connection.getResponseCode()/100)
    {
    case 3:
        url = connection.getHeaderField("Location");
        break;
    case 4:
    case 5:
        // Report some error
        return;
    }
}

Now, in case 2 I want to identify the content-type. For example:

I cannot use the URL structure in order to determine that. For example:

I know how to read the content using an InputStream object, but:

  1. I wish to avoid fetching the content itself if possible.

  2. I don't see how it can help me determine the content-type.

Any ideas would be highly appreciated... thanks.

barak manos
  • 29,648
  • 10
  • 62
  • 114

1 Answers1

2

You could use getContentType:

public String getContentType () Added in API level 1

Returns the MIME-type of the content specified by the response header field content-type or null if type is unknown.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • Question: what string would I get for an `ico` resource? – barak manos Jan 23 '14 at 09:50
  • The registered mime type is `image/vnd.microsoft.icon` see [here](http://www.iana.org/assignments/media-types/image/vnd.microsoft.icon) – Henry Jan 23 '14 at 09:54
  • I get `null` content-type for http://cdn0.sbnation.com/uploads/hub/favicon/247/favicon-0666b2d5.ico, although I clearly see an 'ico' when I connect to this URL through Firefox. Would you know how to explain this? Thanks – barak manos Jan 23 '14 at 11:26
  • The server does not set a `content-type` header in the response for this URL. – Henry Jan 23 '14 at 12:15
  • So is this essentially a server "bug" which is not likely to happen in most servers? – barak manos Jan 23 '14 at 12:20
  • BTW, for `ico` I see that there is an additional type on top of what you already mentioned a few comments above: image/x-icon – barak manos Jan 23 '14 at 14:49