使用HttpClient,总是报出“Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.”的WARN日志,定位到HttpClient的源码如下:

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()可能带来的内存的耗尽问题。

Java-httpClient警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.的更多相关文章

  1. WARN警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended

    使用Apache HttpClient发送请求,有大量WARN警告:Going to buffer response body of large or unknown size. Using getR ...

  2. 关于http客户端常见错误"警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec"

    在开发过程中,经常得写http客户端测试接口服务,今天在使用过程中出现了这样的一个警告: 警告: Going to buffer response body of large or unknown s ...

  3. httpClient使用中报错org.apache.commons.httpclient.HttpMethodBase - Going to buffer response body of large or unknown size.

    在使用HttpClient发送请求,使用httpMethod.getResponseBodyAsString();时当返回值过大时会报错: org.apache.commons.httpclient. ...

  4. java HttpClient操作工具类

    maven: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId> ...

  5. java.lang.IllegalStateException: Cannot forward after response has been committed的一个情况解决方法

    java.lang.IllegalStateException: Cannot forward after response has been committed xxx.xxx.doPost(upd ...

  6. java.io.IOException: Server returned HTTP response code: 411 for URL

    今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误: java.io.IOException: Server returned HTT ...

  7. 通过设置代理,解决服务器禁止抓取,报“java.io.IOException: Server returned HTTP response code: 403 for URL”错误的方法

    java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索 ...

  8. nested exception is java.lang.IllegalStateException: Cannot forward after response has been committed

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...

  9. Error:java: 发现警告, 但指定了 -Werror

    最近在使用IntelliJ IDEA编译Apache Guacamole Web项目时,遇到了一个罕见的bug:"Error:java: 发现警告, 但指定了 -Werror",见 ...

随机推荐

  1. 项目回顾3-再谈图片上传-FormData+ajax上传

    上次在纠结图片上传用base64还是form表单,现在感觉好蠢,因为又开辟了第三条道路. 其实也根本用不到form 只需要一个上传文件的input就好了 <input id="file ...

  2. Saltstack-自动化部署

    Saltstack概述 Salt一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯. salt底层采用动态的连接总线, 使其可以 ...

  3. 一、IOS运行原理

    1.首先执行main函数 2.执行UIPaalicationMain函数 3.UIApplication函数内部 1>创建一个UIApplication实例.这个UIApplication对象是 ...

  4. jquery中$("#afui").get(0)为什么要加get(0)呢?

    jquery中$("#afui").get(0)为什么要加get(0)呢? 2015-04-13 17:46SYYZZ3 | 浏览 509 次  Jquery $("#a ...

  5. 动态调用webservice,不需要添加Web References

    using System; using System.Collections.Generic; using System.Web; using System.Net; using System.IO; ...

  6. heartbeat初探

    1,概念及原理 http://www.mingxiao.info/tag/heartbeat/

  7. Linux Linux程序练习七

    题目:实现两个程序mysignal.mycontrl,mycontrl给mysignal发送SIGINT信号,控制mysignal是否在屏幕打印“hello”字符串. //捕捉信号 #include ...

  8. Jquery easyui tree的使用

    这个ui用的一切都是json数据.树也是如此! 后台需要返回与格式匹配的json数据才能正确加载树. 页面定义一个ui: <ul id="messageInfoAddTree" ...

  9. C#中小数点后保留两位小数,四舍五入的函数及使用方法

    Math.Round(45.367,2)     //Returns   45.37 Math.Round(45.365,2)     //Returns   45.36 C#中的Round()不是我 ...

  10. JAVA中获取当前系统时间

    一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowStrin ...