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 Bindul Bhowmik <bi...@gmail.com> on 2007/02/15 22:06:12 UTC

Re: IllegalStateException: Content must be set before entity is written

Hello Alessandro,

Moving this discussion to the HttpClient Users list.

On 2/15/07, Alessandro.Kohler@swisscom.com
<Al...@swisscom.com> wrote:
>
> hi there
>
> i'm traing to send a multipart reqest using the InputStreamRequestEntity
> in httpclient 3.0.1.
> this is the code:
>
> note: clientRequest is an incoming HttpServletRequest, and the stream
> used for the InputStreamRequestEntity's constructor is obtained by using
> the commons.FileUpload API.
>
>         ServletFileUpload upload = new ServletFileUpload();
>         FileItemIterator iter = upload.getItemIterator(clientRequest);
>         while (iter.hasNext()) {
>           FileItemStream item = iter.next();
>           String name = item.getFieldName();
>           if (item.isFormField()) {
>             postMethod.addParameter(name,
> Streams.asString(item.openStream()));
>           } else {
>             postMethod.setRequestEntity(new
> InputStreamRequestEntity(item.openStream()));
>           }
>         }
>

Can you check if the 'item.openStream()' returns null? The reason the
exception is thrown is that the InputStream passed on to the
InputStreamRequestEntity is null.

> upon executing the post method, the following exception is thrown:
>
> java.lang.IllegalStateException: Content must be set before entity is
> written
>         at
> org.apache.commons.httpclient.methods.InputStreamRequestEntity.writeRequ
> est(InputStreamRequestEntity.java:176)
>         at
> org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequest
> Body(EntityEnclosingMethod.java:495)
>         at
> org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase
> .java:1973)
>         at
> org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java
> :993)
>         at
> org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMe
> thodDirector.java:397)
>         at
> org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMetho
> dDirector.java:170)
>         at
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:3
> 96)
>         at
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:3
> 24)
>
> i'm obviously doing something wrong ;-) .. could someone help me
> understand what?
>
> thanks in advance for any hint!
> cheers
> alessandro
>

Regards,
Bindul

-- 
Bindul Bhowmik
MindTree Consulting Ltd.

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


RE: IllegalStateException: Content must be set before entity is written

Posted by Al...@swisscom.com.
hi Roland & Oleg 

thank you so much for your much appreciated help!

i guess i got cought in the details of the multipart capabilities of the
httpclient api that i simply didn't see i could just pass on the stream
'as is' freom the incoming request.

thanks again, i'll enjoy the weekend much better now ;-)

cheers
alessandro



-----Original Message-----
From: Roland Weber [mailto:http-async@dubioso.net] 
Sent: Friday, February 16, 2007 5:34 PM
To: HttpClient User Discussion
Subject: Re: IllegalStateException: Content must be set before entity is
written

Oleg Kalnichevski wrote:
>> On 2/15/07, Alessandro.Kohler@swisscom.com 
>> <Al...@swisscom.com> wrote:
>>> hi there
>>>
>>> i'm traing to send a multipart reqest using the 
>>> InputStreamRequestEntity in httpclient 3.0.1.
> [...]
> 
> If
> your intention is to post all file items as a multipart coded request,

> please consider using MultipartRequestEntity.

Good point. Come to think of it: if you are getting a multipart request
in your servlet and want to send it on as a multipart request, why do
you bother parsing it at all? You could pass
servletRequest.getInputStream() to the InputStreamRequestEntity.

cheers,
  Roland



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


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


Re: IllegalStateException: Content must be set before entity is written

Posted by Roland Weber <ht...@dubioso.net>.
Oleg Kalnichevski wrote:
>> On 2/15/07, Alessandro.Kohler@swisscom.com
>> <Al...@swisscom.com> wrote:
>>> hi there
>>>
>>> i'm traing to send a multipart reqest using the 
>>> InputStreamRequestEntity in httpclient 3.0.1.
> [...]
> 
> If
> your intention is to post all file items as a multipart coded request,
> please consider using MultipartRequestEntity.

Good point. Come to think of it: if you are getting a multipart
request in your servlet and want to send it on as a multipart
request, why do you bother parsing it at all? You could pass
servletRequest.getInputStream() to the InputStreamRequestEntity.

cheers,
  Roland



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


RE: IllegalStateException: Content must be set before entity is written

Posted by Oleg Kalnichevski <ol...@apache.org>.
> On 2/15/07, Alessandro.Kohler@swisscom.com
> <Al...@swisscom.com> wrote:
> >
> > hi there
> >
> > i'm traing to send a multipart reqest using the 
> > InputStreamRequestEntity in httpclient 3.0.1.
> > this is the code:
> >
> > note: clientRequest is an incoming HttpServletRequest, and the stream 
> > used for the InputStreamRequestEntity's constructor is obtained by 
> > using the commons.FileUpload API.
> >
> >         ServletFileUpload upload = new ServletFileUpload();
> >         FileItemIterator iter = upload.getItemIterator(clientRequest);
> >         while (iter.hasNext()) {
> >           FileItemStream item = iter.next();
> >           String name = item.getFieldName();
> >           if (item.isFormField()) {
> >             postMethod.addParameter(name, 
> > Streams.asString(item.openStream()));
> >           } else {
> >             postMethod.setRequestEntity(new 
> > InputStreamRequestEntity(item.openStream()));
> >           }
> >         }
> >

Alessandro,

This bit of code does not look right to me. You are looping through all
available file items calling postMethod.setRequestEntity() on each
individual item. Please note the postMethod.setRequestEntity() method is
meant to provide the complete request body. So, essentially you end up
with HttpClient trying to post the content of the last file item. If
your intention is to post all file items as a multipart coded request,
please consider using MultipartRequestEntity.

Oleg


> 
> Can you check if the 'item.openStream()' returns null? The reason the
> exception is thrown is that the InputStream passed on to the
> InputStreamRequestEntity is null.
> 
> > upon executing the post method, the following exception is thrown:
> >
> > java.lang.IllegalStateException: Content must be set before entity is 
> > written
> >         at
> > org.apache.commons.httpclient.methods.InputStreamRequestEntity.writeRe
> > qu
> > est(InputStreamRequestEntity.java:176)
> >         at
> > org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeReque
> > st
> > Body(EntityEnclosingMethod.java:495)
> >         at
> > org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBa
> > se
> > .java:1973)
> >         at
> > org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.ja
> > va
> > :993)
> >         at
> > org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(Http
> > Me
> > thodDirector.java:397)
> >         at
> > org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMet
> > ho
> > dDirector.java:170)
> >         at
> > org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java
> > :3
> > 96)
> >         at
> > org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java
> > :3
> > 24)
> >
> > i'm obviously doing something wrong ;-) .. could someone help me 
> > understand what?
> >
> > thanks in advance for any hint!
> > cheers
> > alessandro
> >
> 
> Regards,
> Bindul
> 
> --
> Bindul Bhowmik
> MindTree Consulting Ltd.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 
> 


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


Re: IllegalStateException: Content must be set before entity is written

Posted by Roland Weber <ht...@dubioso.net>.
Hi Alessandro,

> the inputstream being passed to the constructor is not null. i've
> checked this by stepping through the code.

I know it isn't null. You would have gotten a different exception.
My question was: do you see an IOException in the log?

You can disable the auto-buffering behaviour by passing a content
length of -1 to the constructor:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/methods/InputStreamRequestEntity.html#InputStreamRequestEntity(java.io.InputStream,%20long)

You can use this constant if you prefer to avoid hard-coded values:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/methods/EntityEnclosingMethod.html#CONTENT_LENGTH_CHUNKED

This assumes you are sending with HTTP/1.1.

hope that helps,
  Roland


> -----Original Message-----
> From: Roland Weber [mailto:http-async@dubioso.net] 
> Sent: Friday, February 16, 2007 4:57 PM
> To: HttpClient User Discussion
> Subject: Re: IllegalStateException: Content must be set before entity is
> written
> 
> Hello Alessandro,
> 
>> by reading the bytes from 'item.openStream()' in a loop and logging 
>> the number of bytes processed i was able to verify that the 
>> 'item.openStream()' correctly returns the file content.
>> (i did this just befor the line where i would pass the stream to the
>> entity)
>>
>> the InputStreamRequestEntity's constructor seems to not be able to get
> 
>> the contents of the stream passed as argument ....?
> 
> It's not the constructor, but a private method called bufferContent():
> 
>             } catch (IOException e) {
>                 LOG.error(e.getMessage(), e);
>                 this.buffer = null;
>                 this.content = null;
>                 this.contentLength = 0;
>             }
> 
> Do you see an IOException in the log? The constructor would throw an
> IllegalArgumentException if the InputStream is null.
> 
> cheers,
>   Roland
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 


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


RE: IllegalStateException: Content must be set before entity is written

Posted by Al...@swisscom.com.
hi roland

the inputstream being passed to the constructor is not null. i've
checked this by stepping through the code.
the little loop i wrote (which was executed in place of the entity
constructor) was able to read the bytes from the stream with no problem
..

cheers
alessandro



-----Original Message-----
From: Roland Weber [mailto:http-async@dubioso.net] 
Sent: Friday, February 16, 2007 4:57 PM
To: HttpClient User Discussion
Subject: Re: IllegalStateException: Content must be set before entity is
written

Hello Alessandro,

> by reading the bytes from 'item.openStream()' in a loop and logging 
> the number of bytes processed i was able to verify that the 
> 'item.openStream()' correctly returns the file content.
> (i did this just befor the line where i would pass the stream to the
> entity)
> 
> the InputStreamRequestEntity's constructor seems to not be able to get

> the contents of the stream passed as argument ....?

It's not the constructor, but a private method called bufferContent():

            } catch (IOException e) {
                LOG.error(e.getMessage(), e);
                this.buffer = null;
                this.content = null;
                this.contentLength = 0;
            }

Do you see an IOException in the log? The constructor would throw an
IllegalArgumentException if the InputStream is null.

cheers,
  Roland



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


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


Re: IllegalStateException: Content must be set before entity is written

Posted by Roland Weber <ht...@dubioso.net>.
Hello Alessandro,

> by reading the bytes from 'item.openStream()' in a loop and logging the
> number of bytes processed i was able to verify that the
> 'item.openStream()' correctly returns the file content.
> (i did this just befor the line where i would pass the stream to the
> entity)
> 
> the InputStreamRequestEntity's constructor seems to not be able to get
> the contents of the stream passed as argument ....?

It's not the constructor, but a private method called bufferContent():

            } catch (IOException e) {
                LOG.error(e.getMessage(), e);
                this.buffer = null;
                this.content = null;
                this.contentLength = 0;
            }

Do you see an IOException in the log? The constructor would
throw an IllegalArgumentException if the InputStream is null.

cheers,
  Roland



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


RE: IllegalStateException: Content must be set before entity is written

Posted by Al...@swisscom.com.
 
hi bindul

thanks for moving this request - ididn't realise there was a separate
list for http-client.

in reply to your question:
by reading the bytes from 'item.openStream()' in a loop and logging the
number of bytes processed i was able to verify that the
'item.openStream()' correctly returns the file content.
(i did this just befor the line where i would pass the stream to the
entity)

the InputStreamRequestEntity's constructor seems to not be able to get
the contents of the stream passed as argument ....?

cheers
alessandro



-----Original Message-----
From: Bindul Bhowmik [mailto:bindulbhowmik@gmail.com] 
Sent: Thursday, February 15, 2007 10:06 PM
To: Jakarta Commons Users List; HttpClient User Discussion
Subject: Re: IllegalStateException: Content must be set before entity is
written

Hello Alessandro,

Moving this discussion to the HttpClient Users list.

On 2/15/07, Alessandro.Kohler@swisscom.com
<Al...@swisscom.com> wrote:
>
> hi there
>
> i'm traing to send a multipart reqest using the 
> InputStreamRequestEntity in httpclient 3.0.1.
> this is the code:
>
> note: clientRequest is an incoming HttpServletRequest, and the stream 
> used for the InputStreamRequestEntity's constructor is obtained by 
> using the commons.FileUpload API.
>
>         ServletFileUpload upload = new ServletFileUpload();
>         FileItemIterator iter = upload.getItemIterator(clientRequest);
>         while (iter.hasNext()) {
>           FileItemStream item = iter.next();
>           String name = item.getFieldName();
>           if (item.isFormField()) {
>             postMethod.addParameter(name, 
> Streams.asString(item.openStream()));
>           } else {
>             postMethod.setRequestEntity(new 
> InputStreamRequestEntity(item.openStream()));
>           }
>         }
>

Can you check if the 'item.openStream()' returns null? The reason the
exception is thrown is that the InputStream passed on to the
InputStreamRequestEntity is null.

> upon executing the post method, the following exception is thrown:
>
> java.lang.IllegalStateException: Content must be set before entity is 
> written
>         at
> org.apache.commons.httpclient.methods.InputStreamRequestEntity.writeRe
> qu
> est(InputStreamRequestEntity.java:176)
>         at
> org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeReque
> st
> Body(EntityEnclosingMethod.java:495)
>         at
> org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBa
> se
> .java:1973)
>         at
> org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.ja
> va
> :993)
>         at
> org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(Http
> Me
> thodDirector.java:397)
>         at
> org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMet
> ho
> dDirector.java:170)
>         at
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java
> :3
> 96)
>         at
> org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java
> :3
> 24)
>
> i'm obviously doing something wrong ;-) .. could someone help me 
> understand what?
>
> thanks in advance for any hint!
> cheers
> alessandro
>

Regards,
Bindul

--
Bindul Bhowmik
MindTree Consulting Ltd.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


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