Java-httpclient Warning: Going to buffer response body of large or unknown size. Using Getresponsebodyasstream instead is recommended.

Source: Internet
Author: User

Using HttpClient, always quote "going to buffer response body of large or unknown size. Using Getresponsebodyasstream instead is recommended. " warn log, locate the source code for HttpClient as follows:

 Public Abstract classHttpmethodbaseImplementshttpmethod{ ... Public byte[] Getresponsebody ()throwsIOException {if(Responsebody = =NULL) {InputStream instream=Getresponsebodyasstream (); if(Instream! =NULL) {                LongContentLength =getresponsecontentlength (); if(ContentLength > 2147483647L){                    Throw NewIOException ("Content too large to be buffered:" + contentlength + "bytes");
} intLimit = Getparams (). Getintparameter ("Http.method.response.buffer.warnlimit", 1048576); if(ContentLength = = -1l | | contentlength > (Long) {Log.warn ("Going to buffer response body of large or unknown size. Using Getresponsebodyasstream instead is recommended. ");
} log.debug ("Buffering Response Body"); Bytearrayoutputstream OutStream=NewBytearrayoutputstream (contentlength <= 0L? 4096: (int) contentlength); byteBuffer[] =New byte[4096]; intLen; while(len = instream.read (buffer)) > 0){
Outstream.write (Buffer,0, Len);
} outstream.close (); Setresponsestream (NULL); Responsebody=Outstream.tobytearray (); } } returnresponsebody; }...}

the condition of Warn is ((contentlength = =-1) | | (ContentLength > Limit)), that is, either the HTTP header returned does not specify contentlength, or the contentlength is greater than the upper limit (default is 1M). If you can determine the size of the returned results has no significant impact on the program, this warn can be ignored, you can in the configuration of the log to the httpclient log level to ERROR, do not let it be reported.

Of course, this warning is also meaningful,HttpClient recommends using InputStream getresponsebodyasstream () instead of byte[] Getresponsebody (). For situations where the results are large or unpredictable, you need to use inputstreamgetresponsebodyasstream () to avoid the memory exhaustion problem that byte[] getresponsebody () may bring.

Java-httpclient Warning: Going to buffer response body of large or unknown size. Using Getresponsebodyasstream instead is recommended.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.