Httpclien 4 gzip Post-Data

apache-httpclient-4.x, gzip, http

Solution

HttpClient 4.3 APIs:

HttpEntity entity = EntityBuilder.create()
       .setText("some text")
       .setContentType(ContentType.TEXT_PLAIN)
       .gzipCompress()
       .build();

HttpClient 4.2 APIs:

HttpEntity entity = new GzipCompressingEntity(
     new StringEntity("some text", ContentType.TEXT_PLAIN));

GzipCompressingEntity implementation:

 public class GzipCompressingEntity extends HttpEntityWrapper {

    private static final String GZIP_CODEC = "gzip";

    public GzipCompressingEntity(final HttpEntity entity) {
        super(entity);
    }

    @Override
    public Header getContentEncoding() {
        return new BasicHeader(HTTP.CONTENT_ENCODING, GZIP_CODEC);
    }

    @Override
    public long getContentLength() {
        return -1;
    }

    @Override
    public boolean isChunked() {
        // force content chunking
        return true;
    }

    @Override
    public InputStream getContent() throws IOException {
        throw new UnsupportedOperationException();
    }

    @Override
    public void writeTo(final OutputStream outstream) throws IOException {
        final GZIPOutputStream gzip = new GZIPOutputStream(outstream);
        try {
            wrappedEntity.writeTo(gzip);
        } finally {
            gzip.close();
        }
    }

}

Problem

i'm using httpclient 4. When i use ``` new DecompressingHttpClient(client).execute(method) ``` the client acccepts gzip and decompresses if the server sends gzip. But how can i archieve that the client sends it's data gzipped?

Original source