Java-httpClient警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
2015-05-21 17:23
676 查看
使用HttpClient,总是报出“Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.”的WARN日志,定位到HttpClient的源码如下:
报WARN的条件是((contentLength == -1) || (contentLength > limit)),也就是说,或者是返回的HTTP头没有指定contentLength,或者是contentLength大于上限(默认是1M)。如果能确定返回结果的大小对程序没有显著影响,这个WARN就可以忽略,可以在日志的配置中把HttpClient的日志级别调到ERROR,不让它报出来。
当然,这个警告也是有意义的,HttpClient建议使用InputStream getResponseBodyAsStream()代替byte[] getResponseBody()。对于返回结果很大或无法预知的情况,就需要使用InputStreamgetResponseBodyAsStream(),避免byte[] getResponseBody()可能带来的内存的耗尽问题。
public abstract class HttpMethodBase implements HttpMethod { ... public byte[] getResponseBody() throws IOException { if (responseBody == null) { InputStream instream = getResponseBodyAsStream(); if (instream != null) { long contentLength = getResponseContentLength(); if (contentLength > 2147483647L){ throw new IOException("Content too large to be buffered: " + contentLength + " bytes"); } int limit = getParams().getIntParameter("http.method.response.buffer.warnlimit", 1048576); if (contentLength == -1L || contentLength > (long) 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 <= 0L ? 4096: (int) contentLength); byte buffer[] = new byte[4096]; int len; while ((len = instream.read(buffer)) > 0){ outstream.write(buffer, 0, len); } outstream.close(); setResponseStream(null); responseBody = outstream.toByteArray(); } } return responseBody; } ... }
报WARN的条件是((contentLength == -1) || (contentLength > limit)),也就是说,或者是返回的HTTP头没有指定contentLength,或者是contentLength大于上限(默认是1M)。如果能确定返回结果的大小对程序没有显著影响,这个WARN就可以忽略,可以在日志的配置中把HttpClient的日志级别调到ERROR,不让它报出来。
当然,这个警告也是有意义的,HttpClient建议使用InputStream getResponseBodyAsStream()代替byte[] getResponseBody()。对于返回结果很大或无法预知的情况,就需要使用InputStreamgetResponseBodyAsStream(),避免byte[] getResponseBody()可能带来的内存的耗尽问题。
相关文章推荐
- 关于http客户端常见错误"警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
- 关于http客户端常见错误"警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec"
- Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
- Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
- HttpClient警告“Going to buffer response body of large or unknown size. Using getResponseBodyAsStream i
- Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
- httpClient使用中报错org.apache.commons.httpclient.HttpMethodBase - Going to buffer response body of large or unknown size.
- [HttpClient]Using getResponseBodyAsStream instead is recommended警告的解决方案
- Using getResponseBodyAsStream instead is recommended解决方法
- Using getResponseBodyAsStream instead is recommended解决方法
- httpCient 的getResponseBodyAsStream instead is recommended使用方法
- Downloading files from a server to client, using ASP.Net, when file size is too big for MemoryStream using Generic Handlers (ashx)
- httpclient 在获取返回值时,使用getResponseBody触发警告的问题
- Java JDBC - How to connect to Oracle using Service Name instead of SID
- httpclient 在获取返回值时,使用getResponseBodyAsString触发警告的问题
- How do I get ASP.NET Web API to return JSON instead of XML using Chrome
- Spring 整合 Flex (BlazeDS)无法从as对象 到 Java对象转换的异常:org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Date' to required type 'java.sql.Timestamp' for property 'wfsj'; nested exception is java.lang.Ill
- AS中报Gradle 3.3 requires Java 7 or later to run.You are currently using Java 6.解决办法
- Unable to get value of the property '2': object is null or undefined
- [Drupal] How to get the real path of a node, no matter it is a path or a url alias