Saya pikir contoh kode di tautan ini lebih menarik:
ClientGZipContentCompression.java
Mereka menggunakan HttpRequestInterceptor dan HttpResponseInterceptor
Sampel untuk permintaan:
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}
});
Contoh jawaban:
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
});
new WebRequest().get().to("http://www.example.com/").askForGzip(true).executeSync()
. Secara khusus, metode parseResponse (...) harus sesuai dengan yang Anda cari.