I have written simple to code to get the content-type
of a given URL. To make the processing faster, I made a change to set the request method as HEAD
// Added a random puppy face picture here
// On entering this query in browser (or Poster<mozilla> or Postman<chrome>), the
// content type is shown as image/jpeg
URL url = new URL("http://www.bubblews.com/assets/images/news/521013543_1385596410.jpg");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
String contentType = connection.getContentType();
System.out.println(contentType);
if (!contentType.contains("text/html")) {
System.out.println("NOT TEXT/HTML");
// Do something
}
I am trying to achieve something if it is not text/html
, but when I set the request method as HEAD
, the content-type is shown as text/html
. If I fire the same HEAD
request using Poster
or Postman
, I see the content-type
as image/jpeg
.
So what is it that makes the content-type change in case of this Java code?. Can someone please point out any mistake that I may have made?
Note: I used this post as reference