HTTP Response content type different on HEAD request

Some guy

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

hgoebl

You should probably add an Accept header and/or User-Agent header.

Most web servers deliver different content depending on headers set by the client (e.g. web browser, Java HttpURLConnection, curl, ...). This is especially true for Accept, Accept-Encoding, Accept-Language, User-Agent, Cookie and Referer.

As an example, a web-server might refuse to deliver an image, if the Referer header does not link to an internal page. In your case, the web-server doesn't deliver images if it seems like some robot is crawling it. So if you fake your request like if it's coming from a web-browser, the server might deliver it.

When crawling web-sites, you should respect robots.txt (because you act like a robot). So strictly speaking you should be careful when faking User-Agent when doing a lot of requests or create a big business out of this. I don't know how big web-sites react on such behavior, especially when someone by-passes there business...

Please don't see this as a telling-off. I just wanted to point you to this, so you don't run into trouble. Maybe it's not a problem at all, YMMV.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

ZIP file content type for HTTP request

From Dev

Unexpected content in HTTP response

From Dev

HTTP Request with No HTTP Response

From Dev

Force HTTP response with no Content-type?

From Dev

HTTP Request returns 200 OK but no content in response

From Dev

HTTP Request Response in Different Flow

From Dev

WOFF2 - HTTP Content-Type response header suggestion

From Dev

How to determine Content Type of a HTTP Servlet Request?

From Dev

Setting response content type header in JSONP request?

From Dev

spring boot (mvc) response with different content type encoding on error

From Dev

REST-API Different Content-Type on Error Response

From Dev

Spring @RestController Get Request Content-Type to response json or html

From Dev

HTTP request / response with response from a different server

From Dev

AFNetworking - occasionally get "unacceptable content type: application/json" on http response

From Dev

Websocket protocol request header and response header no content type

From Dev

How to make http request and decode response to concrete type in Elm 0.17?

From Dev

How to get response type of the request without loading the full content

From Dev

Is cookie part of the header of a http request/response? Is it a key value pair? How different is it than Accept or Content-type header keys?

From Dev

HTTP Request with No HTTP Response

From Dev

Force HTTP response with no Content-type?

From Dev

HTTP Request Response in Different Flow

From Dev

How to intercept a call from $http and provide a response accordingly to the type of request?

From Dev

Http request document head

From Dev

How to determine Content Type of a HTTP Servlet Request?

From Dev

Setting response content type header in JSONP request?

From Dev

Spring @RestController Get Request Content-Type to response json or html

From Dev

Override content-type for HEAD request

From Dev

How to make http request and decode response to concrete type in Elm 0.17?

From Dev

Making an array of objects from returned type of http request Promise<Response>

Related Related

  1. 1

    ZIP file content type for HTTP request

  2. 2

    Unexpected content in HTTP response

  3. 3

    HTTP Request with No HTTP Response

  4. 4

    Force HTTP response with no Content-type?

  5. 5

    HTTP Request returns 200 OK but no content in response

  6. 6

    HTTP Request Response in Different Flow

  7. 7

    WOFF2 - HTTP Content-Type response header suggestion

  8. 8

    How to determine Content Type of a HTTP Servlet Request?

  9. 9

    Setting response content type header in JSONP request?

  10. 10

    spring boot (mvc) response with different content type encoding on error

  11. 11

    REST-API Different Content-Type on Error Response

  12. 12

    Spring @RestController Get Request Content-Type to response json or html

  13. 13

    HTTP request / response with response from a different server

  14. 14

    AFNetworking - occasionally get "unacceptable content type: application/json" on http response

  15. 15

    Websocket protocol request header and response header no content type

  16. 16

    How to make http request and decode response to concrete type in Elm 0.17?

  17. 17

    How to get response type of the request without loading the full content

  18. 18

    Is cookie part of the header of a http request/response? Is it a key value pair? How different is it than Accept or Content-type header keys?

  19. 19

    HTTP Request with No HTTP Response

  20. 20

    Force HTTP response with no Content-type?

  21. 21

    HTTP Request Response in Different Flow

  22. 22

    How to intercept a call from $http and provide a response accordingly to the type of request?

  23. 23

    Http request document head

  24. 24

    How to determine Content Type of a HTTP Servlet Request?

  25. 25

    Setting response content type header in JSONP request?

  26. 26

    Spring @RestController Get Request Content-Type to response json or html

  27. 27

    Override content-type for HEAD request

  28. 28

    How to make http request and decode response to concrete type in Elm 0.17?

  29. 29

    Making an array of objects from returned type of http request Promise<Response>

HotTag

Archive