I’m using org.apache.commons.httpclient.HttpClient and need to setup response encoding (for some reason server returns incorrect encoding in Content-Type). My way is to get response as raw bytes and convert to String with desired encoding. I’m wondering if there is some better way to do this (eg. setup HttpClient). Thanks for suggestions.
I’m using org.apache.commons.httpclient.HttpClient and need to setup response encoding (for some reason server returns
Share
I don’t think there’s a better answer using
HttpClient3.x APIs.The HTTP 1.1 spec says clearly that a client “must” respect the character set specified in the response header, and use ISO-8859-1 if no character set is specified. The
HttpClientAPIs are designed on the assumption that the programmer wants to conform to the HTTP specs. Obviously, you need to break the rules in the spec so that you can talk to the non-compliant server. Not withstanding, this is not a use-case that the API designers saw a need to support explicitly.If you were using the
HttpClient4.x, you could write your ownResponseHandlerto convert the body into anHttpEntity, ignoring the response message’s notional character set.