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:
When connecting to http://www.google.com, the content-type is Text.
When connecting to http://www.google.com/favicon.ico, the content-type is Image.
I cannot use the URL structure in order to determine that. For example:
- When connecting to http://www.advertising.com/favicon.ico, the response-code is 200, but the content-type is Text (the content itself is "The requested file favicon.ico was not found.").
I know how to read the content using an InputStream
object, but:
I wish to avoid fetching the content itself if possible.
I don't see how it can help me determine the content-type.
Any ideas would be highly appreciated... thanks.
You could use
getContentType
: