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 Jean-Marc Spaggiari <je...@spaggiari.org> on 2013/01/01 20:32:00 UTC

Limit respons size?

Hi,

Is there a way to limit the respons size?

Like, if I call http://my.domain.com/my/page and it's trying to return
me a 5GB ISO file, I don't want to consume that. I want to abort the
call as soon as it's over a certain amount of bytes.

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

Thanks,

JM

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


Re: Limit respons size?

Posted by Jean-Marc Spaggiari <je...@spaggiari.org>.
But the probleme is that I can't trust the server. I need to make sure
that the client is robust so in case server is failing or not
supporting the HTTP range request, client is still exiting after a
certain quantity of bytes received...

2013/1/1, James Shaw <js...@zepler.net>:
> On 1 January 2013 19:32, Jean-Marc Spaggiari <je...@spaggiari.org>
> wrote:
>> Hi,
>>
>> Is there a way to limit the respons size?
>>
>> Like, if I call http://my.domain.com/my/page and it's trying to return
>> me a 5GB ISO file, I don't want to consume that. I want to abort the
>> call as soon as it's over a certain amount of bytes.
>>
>> If I do response.getEntity() is it going to give the control back only
>> when ALL the data will be retreived?
>>
> You could take a look at HTTP range requests, if you know that the
> server will support it.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

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


Re: Limit respons size?

Posted by James Shaw <js...@zepler.net>.
On 1 January 2013 19:32, Jean-Marc Spaggiari <je...@spaggiari.org> wrote:
> Hi,
>
> Is there a way to limit the respons size?
>
> Like, if I call http://my.domain.com/my/page and it's trying to return
> me a 5GB ISO file, I don't want to consume that. I want to abort the
> call as soon as it's over a certain amount of bytes.
>
> If I do response.getEntity() is it going to give the control back only
> when ALL the data will be retreived?
>
You could take a look at HTTP range requests, if you know that the
server will support it.

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


Re: Limit respons size?

Posted by Jean-Marc Spaggiari <je...@spaggiari.org>.
I think I found...

By replacing Void by HttpResponse and returning the response instead of null...

Thanks! I will continue to dig that way.

JM

2013/1/4, Jean-Marc Spaggiari <je...@spaggiari.org>:
> Hi Sabastiano,
>
> Thanks for sharing.
>
> Previously I was doing:
> HttpResponse respons = client.execute (method);
>
> With you code, it's not returning anything. How should I get the
> HttpResponse from this?
>
> Thanks,
>
> JM
>
> 2013/1/1, Sebastiano Vigna <vi...@di.unimi.it>:
>> On 1 Jan 2013, at 11:32 AM, Jean-Marc Spaggiari <je...@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
>>
>>
>

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


Re: Limit respons size?

Posted by Jean-Marc Spaggiari <je...@spaggiari.org>.
Hi Sabastiano,

Thanks for sharing.

Previously I was doing:
HttpResponse respons = client.execute (method);

With you code, it's not returning anything. How should I get the
HttpResponse from this?

Thanks,

JM

2013/1/1, Sebastiano Vigna <vi...@di.unimi.it>:
> On 1 Jan 2013, at 11:32 AM, Jean-Marc Spaggiari <je...@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
>
>

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


Re: Limit respons size?

Posted by Sebastiano Vigna <vi...@di.unimi.it>.
On 1 Jan 2013, at 11:32 AM, Jean-Marc Spaggiari <je...@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