apache httpclient 如何支持br解码
时间: 2024-06-08 17:05:25 浏览: 183
httpclient.zip
要让 Apache HttpClient 支持 BR(Brotli)压缩算法的解码,需要进行以下步骤:
1. 添加 BrotliDecoder 类的引用:
```java
import org.apache.http.client.entity.BrotliInputStream;
```
2. 创建 HttpClient 对象时,注册 Brotli 相关的解码器:
```java
BrotliDecoder brotliDecoder = new BrotliDecoder();
HttpClient httpClient = HttpClientBuilder.create()
.setContentDecoderRegistry(
RegistryBuilder.<InputStreamFactory>create()
.register("br", new BrotliInputStreamFactory(brotliDecoder))
.build())
.build();
```
3. 定义一个 BrotliInputStreamFactory 类,用于创建 BrotliInputStream:
```java
public class BrotliInputStreamFactory implements InputStreamFactory {
private final BrotliDecoder brotliDecoder;
public BrotliInputStreamFactory(BrotliDecoder brotliDecoder) {
this.brotliDecoder = brotliDecoder;
}
@Override
public InputStream create(InputStream inputStream) throws IOException {
return new BrotliInputStream(inputStream, brotliDecoder);
}
}
```
完成以上步骤后,HttpClient 就可以自动识别并解码 BR 压缩的响应了。
阅读全文