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.