How to find HTTP Media Type (MIME type) from response?

Arvind picture Arvind · Jan 31, 2012 · Viewed 24.4k times · Source

While issuing a GET request using Apache HTTP Client v4, how do I obtain the response media type (formally MIME type)?

Using Apache HTTP Client v3, the MIME type was obtained with:

 String mimeType = response.getMimeType();

How do I get the media type using Apache HTTP Client v4?

Answer

peppered picture peppered · Jul 29, 2013

To get content type from response you can use ContentType class.

HttpEntity entity = response.getEntity();
ContentType contentType;
if (entity != null) 
    contentType = ContentType.get(entity);

Using this class you can easily extract mime type:

String mimeType = contentType.getMimeType();

or charset:

Charset charset = contentType.getCharset();