hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject RE: IllegalStateException: Content must be set before entity is written
Date Fri, 16 Feb 2007 16:21:56 GMT

> On 2/15/07, Alessandro.Kohler@swisscom.com
> <Alessandro.Kohler@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


Mime
View raw message