Going to buffer response body of large or unknown size. Using getResponseBodyAsS

使用HttpClient,总是报出“Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.”的WARN日志,定位到HttpClient的源码如下:
public byte [ ] getResponseBody ( ) throws IOException {
if ( this . responseBody == null ) {
InputStream instream = getResponseBodyAsStream ( ) ;
if ( instream != null ) {
long contentLength = getResponseContentLength ( ) ;
if ( contentLength > Integer . MAX_VALUE ) { //guard below cast from overflow
throw new IOException ( "Content too large to be buffered: " + contentLength + " bytes" ) ;
}
int limit = getParams ( ) . getIntParameter ( HttpMethodParams. BUFFER_WARN_TRIGGER_LIMIT , 1024 * 1024 ) ;
if ( ( contentLength == - 1 ) || ( contentLength > limit ) ) {
LOG. warn ( "Going to buffer response body of large or unknown size. "
+ "Using getResponseBodyAsStream instead is recommended." ) ;
}
LOG. debug ( "Buffering response body" ) ;
ByteArrayOutputStream outstream = new ByteArrayOutputStream (
contentLength > 0 ? ( int ) contentLength : DEFAULT_INITIAL_BUFFER_SIZE ) ;
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 ( ) ;
}
}
return this . responseBody ;
}

报WARN的条件是 ((contentLength == -1) || (contentLength > limit)),也就是说,或者是返回的HTTP头没有指定contentLength,或者是contentLength大于上限(默认是1M)。如果 能确定返回结果的大小对程序没有显著影响,这个WARN就可以忽略,可以在日志的配置中把HttpClient的日志级别调到ERROR,不让它报出来。
当然,这个警告也是有意义的,HttpClient建议使用InputStream getResponseBodyAsStream()代替byte[] getResponseBody()。对于返回结果很大或无法预知的情况,就需要使用InputStream getResponseBodyAsStream(),避免byte[] getResponseBody()可能带来的内存的耗尽问题。
使用 String response = method.getResponseBodyAsString().trim(); 获得getMethod返回的内容,
每次都出现如下警告:
[WARN org.apache.commons.httpclient.HttpMethodBase] Going to buffer response body of
large or unknown size. Using getResponseBodyAsStream instead is recommended.
解决方法:
  1. //String response = method.getResponseBodyAsString().trim();
  2. InputStream resStream = method.getResponseBodyAsStream();
  3. BufferedReader br = new BufferedReader( new InputStreamReader(resStream));
  4. StringBuffer resBuffer = new StringBuffer();
  5. String resTemp = "" ;
  6. while ((resTemp = br.readLine()) != null ){
  7. resBuffer.append(resTemp);
  8. }
  9. String response = resBuffer.toString();

//String response = method.getResponseBodyAsString().trim(); InputStream resStream = method.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(resStream)); StringBuffer resBuffer = new StringBuffer(); String resTemp = ""; while((resTemp = br.readLine()) != null){ resBuffer.append(resTemp); } String response = resBuffer.toString();


response
.
addHeader
(
"Content-Type"
,
"text/html; charset=utf-8"
);

 response
.
addHeader
(
"Content-Length"
,
 
String
.
valueOf
(
output
.
getBytes
(
"utf8"
).
length
));

 



 

你可能感兴趣的:(response)