`
liuxinglanyue
  • 浏览: 557760 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

HttpClient的“Going to buffer response body of large or unknown size. Using getRes

 
阅读更多

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

分享到:
评论

相关推荐

    可用org.apache.commons.httpclient-3.1.0.jar.zip

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....

    HttpClient问题:The server failed to respond with a valid HTTP resp

    在使用Apache HttpClient进行HTTP通信时,可能会遇到"HttpClient问题:The server failed to respond with a valid HTTP resp"这样的异常。这个错误通常表示服务器未能返回一个有效的HTTP响应,这可能是由多种原因...

    HttpClient 卡死 response 为 null - 帐前卒 - CSDN1

    在使用Apache HttpClient库进行HTTP请求时,可能会遇到`HttpClient`返回`response`为`null`的问题,这通常意味着请求没有成功地完成或者某些资源没有被正确处理。在本篇文章中,我们将深入探讨这个问题的原因和解决...

    HttpClient 3.x to HttpComponents HttpClient 4.x

    例如,在HttpClient 3.x中,代码可能会使用`***mons.httpclient.HttpClient`类和`***mons.httpclient.methods.GetMethod`等,而在4.x版本中,这些都被新的API所替代。程序员需要熟悉`org.apache....

    commons-httpclient-3.1jar包下载

    http://jakarta.apache.org/commons/httpclient/ org.apache.commons.httpclient.URI org.apache.commons.httpclient.Wire org.apache.commons.httpclient.Cookie org.apache.commons.httpclient.Header org.apache.commons...

    httpclient4.5.3 jar完整包含所有依赖包

    Please note that as of 4.4 HttpClient requires Java 1.6 or newer. Changelog: ------------------- * [HTTPCLIENT-1803] Improved handling of malformed paths by URIBuilder. Contributed by Oleg ...

    Java中Httpclient需要的jar包(httpclient.jar,httpcore.jar及commons-logging.jar)

    HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); System.out.println("Status Code: " + statusCode); // 进一步处理响应内容... } } ``...

    org.apache.commons.httpclient-3.1.jar

    《Apache Commons HttpClient 3.1:HTTP客户端编程的基石》 ...然而,需要注意的是,HttpClient 3.1已不再维护,最新的稳定版本为HttpClient 4.x,对于新项目建议使用更现代的版本以获取更好的性能和兼容性。

    使HttpClient能处理错误ResponseHeader的响应信息。

    本文将深入探讨如何在遇到错误的Response Header时,使HttpClient能够正确地处理响应信息。 首先,我们需要理解Response Header在HTTP通信中的作用。它包含了服务器返回给客户端的元数据,如HTTP状态码、内容类型、...

    HttpClient包及例子

    CloseableHttpResponse response = httpClient.execute(httpGet); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } finally { ...

    httpclient httpclient.jar

    4. 执行请求:`CloseableHttpResponse response = httpClient.execute(httpGet);` 5. 处理响应:`HttpEntity entity = response.getEntity();`,可以读取响应体内容。 6. 关闭资源:`response.close();`,释放连接。...

    httpclient-4.5.2.jar.zip

    CloseableHttpResponse response = httpClient.execute(httpGet); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } finally { ...

    httpclient.jar包下载.zip

    HttpResponse response = httpClient.execute(httpGet); // 处理响应 } catch (Exception e) { e.printStackTrace(); } } } ``` 对于POST请求,你需要创建一个`HttpPost`对象,并使用`setEntity`方法添加请求...

    HttpClient所需jar包(全) httpClient.4.13jar

    CloseableHttpResponse response = httpClient.execute(httpGet); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } finally { ...

    org.apache.commons.httpclient相关资源包

    1. **HttpClient的使用**:如何创建HttpClient实例,设置请求参数,如URL、方法类型、超时等。 2. **请求执行**:发起GET和POST请求,传递参数,处理重定向和自动登录。 3. **响应处理**:解析HTTP响应状态码,获取...

    org.apache.commons.httpclient

    1. **初始化HttpClient**:创建HttpClient实例,配置连接管理器,设置超时和重试策略。 2. **构建请求**:使用`HttpGet`或`HttpPost`对象创建HTTP请求,设置URL,添加请求头和请求参数。 3. **执行请求**:通过...

    org.apache.commons.httpclient 远程下载文件

    import org.apache.commons.httpclient.methods.GetMethod; import java.io.FileOutputStream; import java.io.IOException; public class ServletTest { public static void main(String[] args) { String ...

    org.apache.commons.httpclient相关架包

    标题中的"org.apache.commons.httpclient相关架包"指的是这个库的一系列组件,主要包含在`httpclient.jar`文件中。这个JAR文件包含了HttpClient库的所有必需类和资源,可以被导入到Java项目中以实现HTTP通信功能。 ...

    httpClient

    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient....

    HttpClient实现POST GET和文件下载

    CloseableHttpResponse response = httpClient.execute(httpGet); // 处理响应... ``` 2. POST请求: 对于POST请求,我们使用HttpPost类,通过setEntity方法设置请求体。例如,假设我们要发送JSON数据: ```...

Global site tag (gtag.js) - Google Analytics