This code snippet show you how to get the content type of a result of executing an Http Get request. TheContentType
can be obtained by using ContentType.getOrDefault()
method and passing an HttpEntity
as the arguments. The HttpEntity
can be obtained from the HttpResponse
object.
From the ContentType
object we can get the mime-type by calling the getMimeType()
method. This method will return a string value.
To get the charset we can call the getCharset()
method which will return a java.nio.charset.Charset
object.
You need Apache HTTP Components Library which you can either download or add to your Maven’s pom.xml file.
Here is a Maven Dependency for your project:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency>
package crunchify.com.java.tutorials; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.HttpClientBuilder; import java.io.IOException; import java.nio.charset.Charset; /** * @author Crunchify.com * How to Get Entity ContentType in HttpClient? - response.getEntity() * */ public class CrunchifyGetHTTPContentType { public static void main(String[] args) { CrunchifyGetHTTPContentType crunchifyDemo = new CrunchifyGetHTTPContentType(); crunchifyDemo.requestCrunchifyPage(); crunchifyDemo.requestCrunchifyLogo(); } public void requestCrunchifyPage() { HttpClient crunchifyClient = HttpClientBuilder.create().build(); HttpGet crunchifyRequest = new HttpGet("https://crunchify.com"); try { HttpResponse crunchifyResponse = crunchifyClient.execute(crunchifyRequest); HttpEntity crunchifyEntity = crunchifyResponse.getEntity(); showCrunchifyContentType(crunchifyEntity); } catch (IOException e) { e.printStackTrace(); } } public void requestCrunchifyLogo() { HttpClient crunchifyClient = HttpClientBuilder.create().build(); HttpGet crunchifyRequest = new HttpGet("https://crunchify.com/favicon.ico"); try { HttpResponse crunchifyResponse = crunchifyClient.execute(crunchifyRequest); HttpEntity crunchifyEntity = crunchifyResponse.getEntity(); showCrunchifyContentType(crunchifyEntity); } catch (IOException e) { // printStackTrace() Prints this throwable and its backtrace to the standard error stream. // This method prints a stack trace for this Throwable object on the error output stream that is the value of the field System.err. // The first line of output contains the result of the toString() method for this object. // Remaining lines represent data previously recorded by the method fillInStackTrace(). e.printStackTrace(); } } private void showCrunchifyContentType(HttpEntity entity) { ContentType contentType = ContentType.getOrDefault(entity); String mimeType = contentType.getMimeType(); // Charset: A named mapping between sequences of sixteen-bit Unicode code units and sequences of bytes. // This class defines methods for creating decoders and encoders and for retrieving the various names associated with a charset. // Instances of this class are immutable. Charset charset = contentType.getCharset(); System.out.println("\nMimeType = " + mimeType); System.out.println("Charset = " + charset); } }
Eclipse Console Result:
Just run above program in Eclipse IDE and you should see result as below.
MimeType = text/html Charset = UTF-8 MimeType = image/x-icon Charset = null
Let me know if you face any issue or exception while running above Java program.