Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache Commons HttpAsyncClient是否支持GZIP?_Java_Apache Httpclient 4.x_Apache Httpasyncclient - Fatal编程技术网

Java Apache Commons HttpAsyncClient是否支持GZIP?

Java Apache Commons HttpAsyncClient是否支持GZIP?,java,apache-httpclient-4.x,apache-httpasyncclient,Java,Apache Httpclient 4.x,Apache Httpasyncclient,这个问题是针对Apache Commons HttpClient提出的,但是我使用的是异步客户端HttpAsyncClient 内容解压缩(gzip)不是现成的 我尝试使用以下配置它: httpClientAsync=HttpAsyncClients.custom() .setMaxConnPerRoute(100) .SetMaxConntTotal(200) //启用响应内容编码(gzip) // //注意:由于某些原因不起作用 .addInterceptorLast(ResponseCon

这个问题是针对Apache Commons HttpClient提出的,但是我使用的是异步客户端
HttpAsyncClient

内容解压缩(gzip)不是现成的

我尝试使用以下配置它:

httpClientAsync=HttpAsyncClients.custom()
.setMaxConnPerRoute(100)
.SetMaxConntTotal(200)
//启用响应内容编码(gzip)
//
//注意:由于某些原因不起作用
.addInterceptorLast(ResponseContentEncoding())
我从HttpClientBuilder中复制了它,但它不起作用


有什么想法吗?

使用addInterceptorLastaddInterceptorFirst没有效果。
默认情况下,
asyncHttpClient.execute()
将创建一个
BasicAsyncResponseConsumer

BasicAsyncResponseConsumer
将原始
ContentDecoder
复制到缓冲区中,导致
DecompressingEntity。永远不会调用getContent()

org.apache.http.impl.nio.client.CloseableHttpAsyncClient#execute()

我的解决方案是在回调中手动调用
ResponseContentEncoding.process(resp,context)
,以重置HttpEntity

private static final ResponseContentEncoding responseContentEncoding = new ResponseContentEncoding();

HttpClientContext hcc = HttpClientContext.create();
asyncHttpClient.execute(bidreq, hcc, new FutureCallback<HttpResponse>() {
    @Override
    public void completed(HttpResponse result) {
        HttpEntity entity = null;
        String content = null;
        try {
            responseContentEncoding.process(result, hcc);
            entity = result.getEntity();
            if (entity != null) {
                content = EntityUtils.toString(entity, UTF_8);
                log.info(content);
            }
        } catch (Exception e) {
            log.error("error", e);
        } finally {
            EntityUtils.consumeQuietly(entity);
        }
    }

    @Override
    public void failed(Exception ex) {
        log.error("failed", ex);
    }

    @Override
    public void cancelled() { }
});
private static final ResponseContentEncoding ResponseContentEncoding=new ResponseContentEncoding();
HttpClientContext=HttpClientContext.create();
asyncHttpClient.execute(bidreq、hcc、newfuturecallback()){
@凌驾
公共无效已完成(HttpResponse结果){
HttpEntity=null;
字符串内容=null;
试一试{
responseContentEncoding.过程(结果,hcc);
entity=result.getEntity();
如果(实体!=null){
content=EntityUtils.toString(实体,UTF_8);
日志信息(内容);
}
}捕获(例外e){
日志错误(“错误”,e);
}最后{
EntityUtils.consumer(实体);
}
}
@凌驾
公共作废失败(例外情况除外){
日志错误(“失败”,ex);
}
@凌驾
公共无效已取消(){}
});

至少有一些与此相关的错误报告。阅读文档,但它没有。应该有人站出来为这个项目贡献力量。
    protected void onContentReceived(
            final ContentDecoder decoder, final IOControl ioControl) throws IOException {
        Asserts.notNull(this.buf, "Content buffer");
        this.buf.consumeContent(decoder);
    }
private static final ResponseContentEncoding responseContentEncoding = new ResponseContentEncoding();

HttpClientContext hcc = HttpClientContext.create();
asyncHttpClient.execute(bidreq, hcc, new FutureCallback<HttpResponse>() {
    @Override
    public void completed(HttpResponse result) {
        HttpEntity entity = null;
        String content = null;
        try {
            responseContentEncoding.process(result, hcc);
            entity = result.getEntity();
            if (entity != null) {
                content = EntityUtils.toString(entity, UTF_8);
                log.info(content);
            }
        } catch (Exception e) {
            log.error("error", e);
        } finally {
            EntityUtils.consumeQuietly(entity);
        }
    }

    @Override
    public void failed(Exception ex) {
        log.error("failed", ex);
    }

    @Override
    public void cancelled() { }
});