You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Jiri Pokorny <py...@gmail.com> on 2014/06/06 10:58:15 UTC

Forcing http client to read raw response body (ignore chunked transfer encoding)

Hello,
I am coping with some 3rd party HTTP API which unfortunately includes some
useful information into chunk-extension which is part of chunked transfer
encoding (as defined in RFC2616). However all java libraries for HTTP
access ignores this data (which is probably right).

So I'll need to process these data by myself and I certainly don't want to
implement my own HTTP client. Can you help me or at least give me some
pointer on how to force Apache Http Client to interpret chunked transfer as
raw data (identity)? I have tried this:

http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/201301.mbox/%3C1359388867.10617.16.camel@ubuntu%3E

But it didn't work and also I would prefer solution for 4.3. Thanks in
advance.

Regards,
Jiri Pokorny

Re: Forcing http client to read raw response body (ignore chunked transfer encoding)

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2014-06-06 at 10:58 +0200, Jiri Pokorny wrote:
> Hello,
> I am coping with some 3rd party HTTP API which unfortunately includes some
> useful information into chunk-extension which is part of chunked transfer
> encoding (as defined in RFC2616). However all java libraries for HTTP
> access ignores this data (which is probably right).
> 
> So I'll need to process these data by myself and I certainly don't want to
> implement my own HTTP client. Can you help me or at least give me some
> pointer on how to force Apache Http Client to interpret chunked transfer as
> raw data (identity)? I have tried this:
> 
> http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/201301.mbox/%3C1359388867.10617.16.camel@ubuntu%3E
> 
> But it didn't work and also I would prefer solution for 4.3. Thanks in
> advance.
> 
> Regards,
> Jiri Pokorny

Jiri

Try this
---
HttpProcessor httpproc = HttpProcessorBuilder.create()
    .add(new RequestContent())
    .add(new RequestTargetHost())
    .add(new RequestConnControl())
    .add(new RequestUserAgent("Test/1.1"))
    .add(new RequestExpectContinue(true)).build();

HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

HttpCoreContext coreContext = HttpCoreContext.create();
HttpHost host = new HttpHost("localhost", 8080);
coreContext.setTargetHost(host);

StrictContentLengthStrategy customConLenStrategy = new
StrictContentLengthStrategy() {

    @Override
    public long determineLength(final HttpMessage message) throws
HttpException {
        long length = super.determineLength(message);
        if (length == StrictContentLengthStrategy.CHUNKED) {
            return StrictContentLengthStrategy.IDENTITY;
        } else {
            return length;
        }
    }

};

ConnectionReuseStrategy connStrategy =
DefaultConnectionReuseStrategy.INSTANCE;
DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(
        8 * 1024, 8 * 1024, 
        Consts.ASCII.newDecoder(), 
        Consts.ASCII.newEncoder(),
        MessageConstraints.DEFAULT,
        customConLenStrategy,
        StrictContentLengthStrategy.INSTANCE,
        DefaultHttpRequestWriterFactory.INSTANCE,
        DefaultHttpResponseParserFactory.INSTANCE);
try {
    if (!conn.isOpen()) {
        Socket socket = new Socket(host.getHostName(), host.getPort());
        conn.bind(socket);
    }
    BasicHttpRequest request = new BasicHttpRequest("GET", "/stuff");
    httpexecutor.preProcess(request, httpproc, coreContext);
    HttpResponse response = httpexecutor.execute(request, conn,
coreContext);
    httpexecutor.postProcess(response, httpproc, coreContext);
    HttpEntity entity = response.getEntity();
    InputStream instream = entity != null ? entity.getContent() : null;
    if (!connStrategy.keepAlive(response, coreContext)) {
        conn.close();
    }
} finally {
    conn.close();
}
---

Oleg



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