hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sebastiano Vigna <vi...@di.unimi.it>
Subject Re: Limit respons size?
Date Tue, 01 Jan 2013 19:41:38 GMT
On 1 Jan 2013, at 11:32 AM, Jean-Marc Spaggiari <jean-marc@spaggiari.org> wrote:

> If I do response.getEntity() is it going to give the control back only
> when ALL the data will be retreived?


Positively not. We have the same problem and we solve it like this:

httpGet.reset();
httpGet.setURI( url );		
	
try {
	httpClient.execute( httpGet, new ResponseHandler<Void>() {
		@Override
		public Void handleResponse( HttpResponse response ) throws ClientProtocolException, IOException
{
			// Do stuff with your response (headers, etc.)
			HttpEntity entity = response.getEntity();
			if ( entity != null ) {
				final InputStream content = entity.getContent();
				final InputStream limitedStream = ByteStreams.limit( content, maxResponseBodyLength );
				// Do stuff with the limited stream, reading it until EOF.
				if ( content.read() != -1 ) httpGet.abort();
			}
			return null;
		}} );
}
catch( IOException e ) {
	// Process exception
}

ByteStreams from Google Guava.

Ciao,

					seba


---------------------------------------------------------------------
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