简体   繁体   中英

How to know which type of HttpEntity returned

I am using Apache HttpClient API of version 4.3.3. When I execute the following statement it return HttpResponse instance.

HttpResonse response = httpClient.execute(httpRequest);

Executed the following statement to get response entity.

HttpEntity entity = response.getEntity();

Here I want to know which type of entity object returned. Eg: StringEntity, FileEntity, InputStreamEntity, ...

I tried the following but it returns ResponseEntityWrapper

String className = entity.getClass().getName();

Is there a way to know the specific type of the response entity ?

You wont get a Java object straight from the entity, its content is rather something specified by its mime type, which you can get with entity.getContentType().getValue() (as long as the content-type is specified, otherwise you would obviously get a NullPointerException).

The content type is a string like "text/plain" or "text/html". For instance, if you get an "application/json" content type, then you need to to use a "deserializer" (like Gson or Jackson) and entity.getInputStream() as an input for that deserializer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM