Set response encoding using HttpClient 3.1

I use org.apache.commons.httpclient.HttpClientand you need to configure the response encoding (for some reason, the server returns the wrong encoding in the Content-Type). My way is to get the answer as raw bytes and convert to Stringwith the desired encoding. I am wondering if there is a better way to do this (e.g. setup HttpClient). Thanks for the suggestions.

+3
source share
4 answers

I don't think there is a better answer using API HttpClient3.x.

The HTTP 1.1 specification clearly states that the client "must" respect the character set specified in the response header and use ISO-8859-1 if no character set is specified. The APIs are HttpClientdesigned so that the programmer wants to comply with the HTTP specifications. Obviously, you need to break the rules in the specification so that you can talk to an inappropriate server. Unable to withstand this, this is not the case when API designers clearly needed support.

If you used HttpClient4.x, you can write your own ResponseHandlerto convert the body to HttpEntity, ignoring the conditional character set of the response message.

+3
source

A few notes:

  • , . , , . , , Accept and Accept-Charset:

    Accept: text/plain
    Accept-Charset: utf-8
    

    HTTP .

  • 1. , .

  • String ( , , ), . , . , String. , String .

+2

,

Jus , - googling HttpClient UTF-8.

...

response.setContentType("text/html; charset=UTF-8");

+1

: HttpClient, API.

execute, HttpResponse, .getEntity().getContent(). , , , , InputStreamReader.


, , (, HttpClient).

, , : HttpMethod getResponseBodyAsStream(), InputStreamReader. ( , , String, .)

, HttpClient .


/- .

0
source

Source: https://habr.com/ru/post/1795224/


All Articles