hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Harold Rosenberg <hrose...@vmware.com>
Subject Re: HttpAsyncClient with MultipartEnityBuilder
Date Mon, 11 Nov 2013 18:45:20 GMT

Thanks for the advice. I chose option 1 and it works fine. It may not be the most efficient,
but it is sufficient for my purposes. 

Just in case anyone has the same question in the future, here is a code snippet that shows
how I got this to work: 

String boundary = UUID.randomUUID().toString(); 
HttpEntity mpEntity = MultipartEntityBuilder.create() 
.setBoundary("-------------" + boundary) 
.addBinaryBody("image1", imageAsBytes, ContentType.create("image/jpeg"), "image.jpg") 

ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); 

HttpEntity nByteEntity = new NByteArrayEntity (baoStream.toByteArray(), ContentType.MULTIPART_FORM_DATA);

HttpPost httpPost = new HttpPost(urlToPost); 
httpPost.setHeader("Content-Type", "multipart/form-data;boundary=-------------" + boundary);


_httpAsyncClient.execute(httpPost, httpContext, futureCallback); 



----- Original Message -----

From: "Oleg Kalnichevski" <olegk@apache.org> 
To: "HttpClient User Discussion" <httpclient-users@hc.apache.org> 
Sent: Friday, November 8, 2013 1:38:46 PM 
Subject: Re: HttpAsyncClient with MultipartEnityBuilder 

On Fri, 2013-11-08 at 09:45 -0800, Harold Rosenberg wrote: 
> Actually, one minor correction. In the second case, the exception stack is actually slightly
different. It is: 
> The mpEntity isRepeatable = true 
> Exception in thread "main" java.lang.UnsupportedOperationException: Multipart form entity
does not implement #getContent() 
> at org.apache.http.entity.mime.MultipartFormEntity.getContent(MultipartFormEntity.java:92)

> at org.apache.http.entity.HttpEntityWrapper.getContent(HttpEntityWrapper.java:84) 
> at org.apache.http.entity.BufferedHttpEntity.getContent(BufferedHttpEntity.java:83) 
> at Test.main(Test.java:440) 
> So, in this case the exception happens because the call is being passed through. In the
stack trace from the other case, the exception seems to happen when the BufferedHttpEntity
tries to buffer the data. Does this mean that there is no way to send a Multipart entity using
the HttpAsyncClient? 


That's the problem with trying to mix an asynchronous transport with an 
inherently blocking content producer. MultipartFormEntity can only 
produce its content using #writeTo method and cannot be used 
asynchronously. You basically have two options. (1) Write out entity 
content to ByteArrayOutputStream (or similar) and then create 
NByteArrayEntity using the resultant byte array. (2) Create multipart 
entity implementation that can produce its content asynchronously. 

Hope this helps 


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

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message