atom feed1 message in org.apache.hc.httpclient-usersRe: how to change content length stra...
FromSent OnAttachments
Zanelli FrancoJan 28, 2013 8:12 am 
Subject:Re: how to change content length strategy
From:Zanelli Franco (fzan@tecnosens.it)
Date:Jan 28, 2013 8:12:43 am
List:org.apache.hc.httpclient-users

Il 28/01/2013 17:01, Oleg Kalnichevski ha scritto:

On Mon, 2013-01-28 at 16:24 +0100, Zanelli Franco wrote:

Il 28/01/2013 16:07, Oleg Kalnichevski ha scritto:

On Mon, 2013-01-28 at 12:34 +0100, Zanelli Franco wrote:

hi to all, my device for each http request, returns a response that provides more bytes
than specified with Content-Length header. I can know the amount of additional
bytes, but I don't know how to tell to httpclient. Can you please help me? Should I change the content length strategy implementing a new
ContentLengthStratecy class? is it possible?

thank you greetings

I would very strongly to recommend you to not misuse HTTP protocol and do not create HTTP services that grossly violate the specification. Please consider making your code generate properly delineated message and pass whatever additional custom information your particular application requires in a custom header such as 'X-My-Content-Length: 20 bytes shorter or whatever'.

Oleg

PS: yes, it is definitely possible to plug-in a custom ContentLengthStrategy implementation. It is somewhat easier with 4.3 APIs and slightly more difficult with 4.2 APIs.

--------------------------------------------------------------------- To unsubscribe, e-mail: http@hc.apache.org For additional commands, e-mail: http@hc.apache.org

Thanks for your fast reply. Honestly I didn't understand how to force httpclient to consider my custom
content length and not the one specified in device response. By the way, can you suggest me where can i find a sample code to change
ContentLengthStrategy or resolve my issue, please?

Thank you very much

So, you are saying you are not in control of how the device generates response messages? If that is the case, this is how you can work it around

--- PoolingClientConnectionManager cm = new PoolingClientConnectionManager() {

@Override protected ClientConnectionOperator createConnectionOperator(SchemeRegistry schreg) { return new DefaultClientConnectionOperator(schreg) { @Override public OperatedClientConnection createConnection() { return new DefaultClientConnection() { @Override protected EntityDeserializer createEntityDeserializer() { return new EntityDeserializer(new LaxContentLengthStrategy()); } }; }

}; }

}; DefaultHttpClient httpclient = new DefaultHttpClient(cm);

HttpGet httpget = new HttpGet("http://www.google.com/"); HttpResponse response = httpclient.execute(httpget); EntityUtils.consume(response.getEntity());

It is kind of ugly. It will get less cumbersome with 4.3.

Yes, the device is not under my control, so i'll use the code you posted. Thank you, you've been very helpful

Franco