java - 编码 HttpResponse UTF-8

标签 java http httpresponse apache-httpcomponents

我通过方法从 url 获取 HttpResponse:

public void getResponse(String url) {
    String response;
    String str;
    BufferedReader bf;
    GZIPInputStream gzip;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        HttpGet httpGet = new HttpGet(url);

        httpResponse = httpClient.execute(httpGet);
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity, HTTP.UTF_8);

        System.out.println(response);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但是我得到的响应是这样的错误:

�P:�ˎ�#�I���!h����Ab���9�xQ�7۰=b��x<M����n�

我检查了“HttpEntity”的内容编码是“gzip”。 那么,是否有任何建议将响应编码为 utf8?

更新:[已解决] 我用方法解决了问题:

public void getResponse(String url) {
    String response;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        HttpGet httpGet = new HttpGet(url);

        httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
            @Override
            public void process(final HttpResponse response,
               final HttpContext context) throws HttpException,
               IOException {
                    HttpEntity entity = response.getEntity();
                    Header encheader = entity.getContentEncoding();
                    if (encheader != null) {
                        HeaderElement[] codecs = encheader.getElements();
                        for (int i = 0; i < codecs.length; i++) {
                            if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                                response.setEntity(new GzipDecompressingEntity(entity));
                            return;
                            }
                        }
                    }
            }
        });

        httpResponse = httpClient.execute(httpGet);
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity, HTTP.UTF_8);

        System.out.println(response);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

最佳答案

不要使用DefaultHttpClient,使用DecompressingHttpClient,响应将自动解压缩。

参见http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DecompressingHttpClient.html

关于java - 编码 HttpResponse UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31142051/

相关文章:

java - java中如何检测字符串中是否包含URL?

http - 使用 Http 代理与 https 代理的优缺点?

security - 如何在特定域中提取有关 SSL 加密的信息

asp.net-web-api - ASP.NET WebAPI : How to control string content returned to client?

java - Retrofit + Instagram API 在请求访问 token 时返回错误 400

java - 如何从 JButton 获取按下的字母并将其与字符串进行比较

java - 将 2 个 32 位整数交织成 64 位整数

ruby-on-rails - 通过 http POST 在 Ruby on Rails 应用程序中创建对象的最合适方法是什么?

javascript - 在将响应值传递给另一个函数之前,如何专门修改响应值的第一个索引[0]?

java - 将响应 header 添加到 JAX-RS Web 服务