Set response encoding with HttpClient 3.1

前端 未结 4 651
甜味超标
甜味超标 2021-01-14 14:32

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

4条回答
  •  再見小時候
    2021-01-14 14:52

    I don't think there's a better answer using HttpClient 3.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 HttpClient APIs 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 HttpClient 4.x, you could write your own ResponseHandler to convert the body into an HttpEntity, ignoring the response message's notional character set.

提交回复
热议问题