Httpclien 4 gzip Post-Data

wut*_*aer 3 gzip http apache-httpclient-4.x

我正在使用httpclient 4.当我使用时

new DecompressingHttpClient(client).execute(method)
Run Code Online (Sandbox Code Playgroud)

如果服务器发送gzip,客户端会接受gzip并解压缩.

但我怎么能表明客户端发送数据gzip?

ok2*_*k2c 5

HttpClient 4.3 API:

HttpEntity entity = EntityBuilder.create()
       .setText("some text")
       .setContentType(ContentType.TEXT_PLAIN)
       .gzipCompress()
       .build();
Run Code Online (Sandbox Code Playgroud)

HttpClient 4.2 API:

HttpEntity entity = new GzipCompressingEntity(
     new StringEntity("some text", ContentType.TEXT_PLAIN));
Run Code Online (Sandbox Code Playgroud)

GzipCompressingEntity实现:

 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();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)