You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Steve Johnson <sj...@mercury.com> on 2004/12/06 17:24:17 UTC
HTTPClient 2.0 - Limiting response body read length
Hi All,
HTTPClient 2.0 - Limiting response body read length
This code seems to indicate that it is not possible to limit how
much stuff gets read. Is this true?
HttpMethodBase.
public byte[] getResponseBody() {
if (this.responseBody == null) {
try {
InputStream instream = getResponseBodyAsStream();
if (instream != null) {
LOG.debug("Buffering response body");
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int len;
while ((len = instream.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();
setResponseStream(null);
this.responseBody = outstream.toByteArray();
}
} catch (IOException e) {
LOG.error("I/O failure reading response body", e);
this.responseBody = null;
}
}
return this.responseBody;
}
If so, would it be best to override this method with a method
to allow limiting the length like this:
while ((len = instream.read(buffer)) > 0 && totalLength+len < MaxDesiredLength) {
totalLength += len;
outstream.write(buffer, 0, len);
}
Thanks for your time and effort,
Steve Johnson, Software Engineer, sjohnson@mercury.com
direct 720.564.6532
www.mercury.com <http://www.mercury.com/>
<http://www.mercury.com/>
<http://www.mercury.com/>
Re: HTTPClient 2.0 - Limiting response body read length
Posted by Oleg Kalnichevski <ol...@apache.org>.
Steve,
Do not override the method. Better provide a simple helper class to
consume the response body as a stream. See this discussion for
additional pointers:
http://nagoya.apache.org/eyebrowse/ReadMsg?listName=httpclient-
user@jakarta.apache.org&msgNo=103
We are trying to deemphasize the use of HttpMethod#getResponseBody and
HttpMethod#getResponseBodyAsString and deprecate these methods
altogether in 4.0
Oleg
On Mon, 2004-12-06 at 08:24 -0800, Steve Johnson wrote:
> Hi All,
>
>
>
> HTTPClient 2.0 - Limiting response body read length
>
>
>
> This code seems to indicate that it is not possible to limit how
>
> much stuff gets read. Is this true?
>
>
>
> HttpMethodBase.
>
> public byte[] getResponseBody() {
>
> if (this.responseBody == null) {
>
> try {
>
> InputStream instream = getResponseBodyAsStream();
>
> if (instream != null) {
>
> LOG.debug("Buffering response body");
>
> ByteArrayOutputStream outstream = new ByteArrayOutputStream();
>
> byte[] buffer = new byte[4096];
>
> int len;
>
> while ((len = instream.read(buffer)) > 0) {
>
> outstream.write(buffer, 0, len);
>
> }
>
> outstream.close();
>
> setResponseStream(null);
>
> this.responseBody = outstream.toByteArray();
>
> }
>
> } catch (IOException e) {
>
> LOG.error("I/O failure reading response body", e);
>
> this.responseBody = null;
>
> }
>
> }
>
> return this.responseBody;
>
> }
>
>
>
> If so, would it be best to override this method with a method
>
> to allow limiting the length like this:
>
>
>
> while ((len = instream.read(buffer)) > 0 && totalLength+len < MaxDesiredLength) {
>
> totalLength += len;
>
> outstream.write(buffer, 0, len);
>
> }
>
>
>
> Thanks for your time and effort,
>
>
>
> Steve Johnson, Software Engineer, sjohnson@mercury.com
>
> direct 720.564.6532
>
> www.mercury.com <http://www.mercury.com/>
>
> <http://www.mercury.com/>
>
> <http://www.mercury.com/>
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org