How to set ContentType inside an HttpEntity object?

I have the following Java code:

HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
ReadContent = EntityUtils.toString(entity, HTTP.UTF_8);
EntityUtils.consume(entity);

I need that the entity's content is always read as an UTF-8 content type.

Unfortunately EntityUtils.toString method use the second parameter only if the content type is not specified inside the HttpEntity object, instead I need to force to use always the utf-8 content-type.

Any idea?

Upvotes: 0

Views: 1105

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280166

That doesn't make much sense. If the response content was encoded with a character encoding X, decoding it with character encoding Y would cause undefined results.

instead I need to force to use always the utf-8 content-type

I don't recommend this, but you can always get the response's InputStream and process the bytes as you see fit.

InputStream responseContent = entity.getContent();

(Don't forget to close() it.)

Upvotes: 2

Related Questions