You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Arya F <ar...@gmail.com> on 2017/11/10 18:12:13 UTC

[users@httpd] decompressing gzipped response?

I'm using Apache HTTPClient 4.5.3 to make some HTTP requests, but I am
getting a gzipped response back I have tried many things I found online but
non of them worked. I still get gibberish when I print the response. Below
are the relevant code. What do I need to do to get a human readable
response?

static public CloseableHttpClient CreateHttpClient() {
    // return
    // HttpClients.custom().disableAutomaticRetries().setHttpProcessor(HttpProcessorBuilder.create().build())
    // .build();

    return HttpClientBuilder.create().disableAutomaticRetries()
            .setHttpProcessor(HttpProcessorBuilder.create().build()).build();
}

static public RequestConfig GetConfig() {
    return RequestConfig.custom().setSocketTimeout(READTIMEOUT).setConnectTimeout(CONNECTTIMEOUT)
            .setConnectionRequestTimeout(REQUESTTIMEOUT).build();
}

static public String updates() {
    String result = "";
    String url = "https://example.com";
    CloseableHttpClient httpClient = CreateHttpClient();
    CloseableHttpResponse response = null;
    URL urlObj;
    RequestConfig config = GetConfig();

    try {
        urlObj = new URL(url);

        HttpPost request = new HttpPost(url);
        request.setConfig(config);

        StringEntity params = new StringEntity("example");

        request.addHeader("Accept-Language", "en");
        request.addHeader("Content-Type", "application/json; charset=UTF-8");
        request.addHeader("Content-Length",
String.valueOf(params.getContentLength()));
        request.addHeader("Host", urlObj.getHost());
        request.addHeader("Connection", "Keep-Alive");
        request.addHeader("Accept-Encoding", "gzip");

        request.setEntity(params);
        response = httpClient.execute(request);

        int responseCode = response.getStatusLine().getStatusCode();

        System.out.println("updates response code: " + responseCode);


        // BufferedReader rd = new BufferedReader(new
        // InputStreamReader(response.getEntity().getContent(), "UTF-8"));

        result = EntityUtils.toString(response.getEntity());
        // String line = "";
        // while ((line = rd.readLine()) != null) {
        // result.append(line);
        // }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (response != null)
                response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return result;
}

Re: [users@httpd] decompressing gzipped response?

Posted by Yann Ylavic <yl...@gmail.com>.
On Fri, Nov 10, 2017 at 7:12 PM, Arya F <ar...@gmail.com> wrote:
> I'm using Apache HTTPClient 4.5.3 to make some HTTP requests, but I am
> getting a gzipped response back I have tried many things I found online but
> non of them worked.

This question is probably more meant for the
httpclient-users@hc.apache.org mailing list, but I can try a response
based on the HTTP protocol...

>
> static public String updates() {
>     String result = "";
>     String url = "https://example.com";
>     CloseableHttpClient httpClient = CreateHttpClient();
>     CloseableHttpResponse response = null;
>     URL urlObj;
>     RequestConfig config = GetConfig();
>
>     try {
>         urlObj = new URL(url);
>
>         HttpPost request = new HttpPost(url);
>         request.setConfig(config);
>
>         StringEntity params = new StringEntity("example");
>
>         request.addHeader("Accept-Language", "en");
>         request.addHeader("Content-Type", "application/json;
> charset=UTF-8");
>         request.addHeader("Content-Length",
> String.valueOf(params.getContentLength()));
>         request.addHeader("Host", urlObj.getHost());
>         request.addHeader("Connection", "Keep-Alive");
>         request.addHeader("Accept-Encoding", "gzip");

You should try to *not* add this request header which says the
response can be gzip-encoded.
Without it, the server ought to respond with a plain response body.

>
>         request.setEntity(params);
>         response = httpClient.execute(request);
>
>         int responseCode = response.getStatusLine().getStatusCode();
>
>         System.out.println("updates response code: " + responseCode);
>
>
>         // BufferedReader rd = new BufferedReader(new
>         // InputStreamReader(response.getEntity().getContent(), "UTF-8"));
>
>         result = EntityUtils.toString(response.getEntity());
>         // String line = "";
>         // while ((line = rd.readLine()) != null) {
>         // result.append(line);
>         // }
>
>     } catch (Exception e) {
>         e.printStackTrace();
>     } finally {
>         try {
>             if (response != null)
>                 response.close();
>             httpClient.close();
>         } catch (IOException e) {
>             e.printStackTrace();
>         }
>     }
>
>     return result;
> }


Regards,
Yann.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org