Stepped on the access Baidu AI speech recognition cloud service a pit, ContentType not read Charset

The reason: Not all Web servers return the correct content encoding format, then add a misjudged it.

Solution: From reading the less than Charset ContentType.getCharset (), using the default UTF-8


The correct way is to acquire content encoding format:

- calling httpResponse .getEntiry () returns the result to obtain

- calling ContentType.get () Gets the content type

- calling ContentType.getCharset () Gets the encoding format

- calling EntityUtils.toString () will return the result as a string formatted

public class RespStr implements ResponseHandler<String> {
    @Override
    public String handleResponse(HttpResponse httpResponse) throws IOException {
        HttpEntity entity = httpResponse.getEntity();
        if (entity == null) {
            throw new ClientProtocolException("Response contains no content");
        }

        // 读取返回内容
        ContentType contentType = ContentType.getOrDefault(entity);
        Charset charset = contentType.getCharset();
        return EntityUtils.toString(entity, charset == null ? Charset.forName("utf-8") : charset);
    }
}

ResponseHandler <T> is an interface provided within the package httpclient, implementing a function the handleResponse () processing HTTP return results.

Guess you like

Origin blog.51cto.com/13851865/2473642