The Problem
My application uses Apache HttpClient 4.2, but when it sends request to some web pages, the response is garbled characters.
Using Fiddler's Composer to execute the request, found the response is gziped.
Content-Encoding: gzip
My application uses Apache HttpClient 4.2, but when it sends request to some web pages, the response is garbled characters.
Using Fiddler's Composer to execute the request, found the response is gziped.
Content-Encoding: gzip
The Solution
In Apache HttpClient 4.2, the DefaultHttpClient doesn't support compression, so it doesn't decompress the response. We have to use DecompressingHttpClient.
public void usingDefualtHttpClient() throws Exception { // output would be garbled characters in http client 4.2. HttpClient httpClient = new DefaultHttpClient(); getContent(httpClient, new URI(URL_STRING)); } public void usingDecompressingHttpClient() throws Exception { // use DecompressingHttpClient to handle gzip response in http client 4.2. HttpClient httpCLient = new DecompressingHttpClient( new DefaultHttpClient()); getContent(httpCLient, new URI(URL_STRING)); } private void getContent(HttpClient httpClient, URI url) throws IOException, ClientProtocolException { HttpGet httpGet = new HttpGet(url); HttpResponse httpRsp = httpClient.execute(httpGet); String text = EntityUtils.toString(httpRsp.getEntity()); for (Header header : httpRsp.getAllHeaders()) { System.out.println(header); } System.out.println(text); }The problem can also be fixed by upgrading http client to 4.3.5: in this versionthe default http client supports compression.
And in http client to 4.3.5, the DefaultHttpClient is deprecated, it's recommenced to use HttpClientBuilder:
public void usingHttpClientBuilderIn43() throws Exception { HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient httpClient = builder.build(); getContent(httpClient, new URI(URL_STRING)); }