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: HttpAsyncClient Multipart Entity not working?
Date Sat, 09 Jun 2012 11:06:52 GMT
On Fri, 2012-06-08 at 10:43 -0400, rishi.easwaran@aol.com wrote:
> Hi,
> 
> I would like some help, clarification on using HttpAsyncClient with multipart entitity
.
> I got it working with HttpCleint but I need it to work with HttpAsyncClient.
> Blocked on this but any help on this would be great.
> 
> Sample of how I am using it with both Clients.
> 
> MultipartEntity mpe = new MultipartEntity()
> ;
> mpe.addPart("key1", new StringBody("value1"))
> ;
> mpe.addPart("key2", new StringBody("value2"))
> ;
> mpe.addPart("data1", new ByteArrayBody(hugeData1, ""))
> ;
> mpe.addPart("data2", new ByteArrayBody(hugeData2, ""))
> 
> ;Using Blocking I/O: This one works.
> 
> SchemeRegistry schemeRegistry = new SchemeRegistry()
> ;schemeRegistry.register(new Scheme("http", 80, new PlainSocketFactory()))
> ;ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry)
> ;HttpClient httpClient = new DefaultHttpClient(cm)
> ;HttpPost post = new HttpPost(myurl)
> ;post.setEntity(mpe)
> ;HttpResponse r =  client.execute(post)
> ;int status = r.getStatusLine().getStatusCode()
> 
> 
> ;Using NIO: This fails with the following exception. (Multipart form entity does not
implement #getContent())
> 
> HttpParams params = new BasicHttpParams()
> ;AsyncSchemeRegistry schemeRegistry = new AsyncSchemeRegistry()
> ;schemeRegistry.register(new AsyncScheme("http", 80, null))
> ;ClientAsyncConnectionManager cm = new PoolingClientAsyncConnectionManager(new DefaultConnectingIOReactor(),
schemeRegistry)
> ;DefaultHttpAsyncClient asyncClient = new DefaultHttpAsyncClient(cm)
> ;asyncClient.setParams(params)
> ;asyncClient.start();  
> HttpPost post = new HttpPost(myurl)
> ;
> post.setEntity(mpe)
> ;Future<HttpResponse> f= asyncClient.execute(post, null)
> ;// do something
> HttpResponse r = f.get(5, TimeUnit.SECONDS)
> ;int status = r.getStatusLine().getStatusCode(); 
> 
> Thanks,
> 
> Rishi.
> 

Rishi

MultipartEntity is inherently blocking. It can only generate its content
by writing it out to an OutputStream through #writeTo() method. It does
not support #getContent() method, which makes it impossible to use with
a non-blocking I/O model without buffering.

You can wrap the MultipartEntity instance with BufferedHttpEntity in
order to make it compatible with the HttpAsyncClient's non-blocking I/O
model.

Oleg  


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


Mime
View raw message