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 hanasaki jiji <ha...@gmail.com> on 2010/04/26 22:22:17 UTC

HTTP Post without content-length : server failure even when chunked

Using httpclient 4.0.1 and doing an http POST
NOTE: this has been tried with both "chunked = true" and false

The POST code does the following:
            InputStreamEntity uploadHttpEntity =
                    new InputStreamEntity(sourceInputStream, -1);
            uploadHttpEntity.setChunked(true);
            httppost.setEntity(uploadHttpEntity);

Where the InputStream is of an unknown source.  It could be the result
of ByteArrayInputStream, from a file or any other class that resolves
to an "InputStream".  Thus, the "-1" in the InputStreamEntity and the
use of this class vs perhaps a StringEntity.

The server code explicitly looks for the Content-Length header which
is seen as -1 and thus fails thinking that the length is either -1 or
it already has an EOF.  Interestingly a POST with
java.net....HTTPURLConnection works fine.

How can HTTPClient be used and have it send up the length as the
java.net classes do?

Thank you,

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


Re: HTTP Post without content-length : server failure even when chunked

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2010-04-29 at 22:07 -0500, hanasaki wrote:
> Unfortunately my code is hitting a servlet not under my control which 
> depends on the content length being set to the actual number of bytes.
> 
> * note: the content is in an InputStream and thus its full length is not 
> known (could be a file, pipe from someplace else [ex: with 
> IOUtils.copy(input,output], etc..}
> 
> A POST with the java.net classes seems to send up the content length (I 
> do not set the header at all myself) while the httpclient 4.x libs pass 
> up -1 in. 

No, it does not. HttpClient does not generate a content-length header
when content chunking is used.

See it for yourself by turning on the wire logging

http://hc.apache.org/httpcomponents-client-4.0.1/logging.html 


>  The goal is to get httpclient to do what the java.net lib 
> does so I can use httpclient instead of java.net (it is used in all the 
> rest of the code and having two API's makes maintenance a bit harder for 
> some of the team).
> 
> Like it or not, that is the server that needs to be communicated with. 
> Can httpclient be made to function as java.net does?  how?
> 

Wrap the original entity with the BufferedHttpEntity class to buffer the
content in memory. This will enable HttpClient to generate a valid
Content-Length instead of falling back onto more efficient chunk
coding. 

Oleg


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


Re: HTTP Post without content-length : server failure even when chunked

Posted by hanasaki <ha...@gmail.com>.
Unfortunately my code is hitting a servlet not under my control which 
depends on the content length being set to the actual number of bytes.

* note: the content is in an InputStream and thus its full length is not 
known (could be a file, pipe from someplace else [ex: with 
IOUtils.copy(input,output], etc..}

A POST with the java.net classes seems to send up the content length (I 
do not set the header at all myself) while the httpclient 4.x libs pass 
up -1 in.  The goal is to get httpclient to do what the java.net lib 
does so I can use httpclient instead of java.net (it is used in all the 
rest of the code and having two API's makes maintenance a bit harder for 
some of the team).

Like it or not, that is the server that needs to be communicated with. 
Can httpclient be made to function as java.net does?  how?

Thank you,

-------- Original Message --------
Subject: Re: HTTP Post without content-length : server failure even when
chunked
From: Oleg Kalnichevski <ol...@apache.org>
To: HttpClient User Discussion <ht...@hc.apache.org>
Date: 04/27/2010 08:52 AM

> On Mon, 2010-04-26 at 22:03 -0500, hanasaki wrote:
>> The issue seems to be that when the servlet grabs Content-Length
>> header, it gets -1 and the folks who wrote the servlet use that for
>> a new byte[Content-Length].
>
> Oh well, what shall I say?
>
>
>> So with content-length = -1 httpclient will automatically use
>> Chunked coding?
>
> Yes, it will
>
>
>> I manually set it via API anyways. With chunked=true, should the
>> servlet have a content length?
>
> No, it should not
>
>> How SHOULD the servlet be determining how many bytes to read or
>> when to end reading?
>>
>
> One is expected to keep on reading until the end of data stream
> (read operation returns -1).
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
>
>
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>

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


Re: HTTP Post without content-length : server failure even when chunked

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2010-04-26 at 22:03 -0500, hanasaki wrote:
> The issue seems to be that when the servlet grabs Content-Length header, 
> it gets -1 and the folks who wrote the servlet use that for a new 
> byte[Content-Length]. 

Oh well, what shall I say?


>  So with content-length = -1 httpclient will 
> automatically use Chunked coding? 

Yes, it will


>  I manually set it via API anyways. 
> With chunked=true, should the servlet have a content length?

No, it should not

>   How SHOULD 
> the servlet be determining how many bytes to read or when to end reading?
> 

One is expected to keep on reading until the end of data stream (read
operation returns -1).

Oleg



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


Re: HTTP Post without content-length : server failure even when chunked

Posted by hanasaki <ha...@gmail.com>.
The issue seems to be that when the servlet grabs Content-Length header, 
it gets -1 and the folks who wrote the servlet use that for a new 
byte[Content-Length].  So with content-length = -1 httpclient will 
automatically use Chunked coding?  I manually set it via API anyways. 
With chunked=true, should the servlet have a content length?  How SHOULD 
the servlet be determining how many bytes to read or when to end reading?

-------- Original Message --------
Subject: Re: HTTP Post without content-length : server failure even when
chunked
From: Oleg Kalnichevski <ol...@apache.org>
To: HttpClient User Discussion <ht...@hc.apache.org>
Date: 04/26/2010 04:51 PM

> On Mon, 2010-04-26 at 15:22 -0500, hanasaki jiji wrote:
>> Using httpclient 4.0.1 and doing an http POST NOTE: this has been
>> tried with both "chunked = true" and false
>>
>> The POST code does the following: InputStreamEntity
>> uploadHttpEntity = new InputStreamEntity(sourceInputStream, -1);
>> uploadHttpEntity.setChunked(true);
>> httppost.setEntity(uploadHttpEntity);
>>
>> Where the InputStream is of an unknown source.  It could be the
>> result of ByteArrayInputStream, from a file or any other class that
>> resolves to an "InputStream".  Thus, the "-1" in the
>> InputStreamEntity and the use of this class vs perhaps a
>> StringEntity.
>>
>> The server code explicitly looks for the Content-Length header
>> which is seen as -1 and thus fails thinking that the length is
>> either -1 or it already has an EOF.
>
> Setting entity's length to -1 will NOT cause HttpClient to send -1
> in the Content-Length header. HttpClient will automatically use
> chunk coding if the content length is unknown (negative).
>
> Oleg
>
>> Interestingly a POST with java.net....HTTPURLConnection works
>> fine.
>>
>> How can HTTPClient be used and have it send up the length as the
>> java.net classes do?
>>
>> Thank you,
>>
>> ---------------------------------------------------------------------
>>
>>
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail:
>> httpclient-users-help@hc.apache.org
>>
>
>
>
> ---------------------------------------------------------------------
>
>
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>

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


Re: HTTP Post without content-length : server failure even when chunked

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2010-04-26 at 15:22 -0500, hanasaki jiji wrote:
> Using httpclient 4.0.1 and doing an http POST
> NOTE: this has been tried with both "chunked = true" and false
> 
> The POST code does the following:
>             InputStreamEntity uploadHttpEntity =
>                     new InputStreamEntity(sourceInputStream, -1);
>             uploadHttpEntity.setChunked(true);
>             httppost.setEntity(uploadHttpEntity);
> 
> Where the InputStream is of an unknown source.  It could be the result
> of ByteArrayInputStream, from a file or any other class that resolves
> to an "InputStream".  Thus, the "-1" in the InputStreamEntity and the
> use of this class vs perhaps a StringEntity.
> 
> The server code explicitly looks for the Content-Length header which
> is seen as -1 and thus fails thinking that the length is either -1 or
> it already has an EOF. 

Setting entity's length to -1 will NOT cause HttpClient to send -1 in
the Content-Length header. HttpClient will automatically use chunk
coding if the content length is unknown (negative).

Oleg

>  Interestingly a POST with
> java.net....HTTPURLConnection works fine.
> 
> How can HTTPClient be used and have it send up the length as the
> java.net classes do?
> 
> Thank you,
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 



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