• Java-httpClient警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.


    使用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三大特性或java对象的三大特性?
    数据结构与算法第10周作业——二叉树的创建和遍历算法
    JDBC的应用
    数据结构与算法--第5周作业(线性表合并算法)
    数据结构与算法--第4周作业(单链表)
    WEB(JSP)下的JDBC操作实验
    application下的JDBC操作
    思考题:JSP的指令inclue和动作include的区别
    css3动画小试
    JS => 函数
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/4520295.html
Copyright © 2020-2023  润新知