`

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

阅读更多
[WARN ] 2011-04-19 21:38:01 Method: org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:682)
Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
[WARN ] 2011-04-19 21:38:22 Method: org.apache.commons.httpclient.HttpMethodBase.getResponseBody(HttpMethodBase.java:682)
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 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();
分享到:
评论

相关推荐

    Introduction.to.Game.Programming.Using.Csharp.and.Unity.3D

    This book is written with two objective in mind, first, to introduce the reader to the concepts of programming using C#, second, to put into practice the concepts in a fun and entertaining way by ...

    Build.Your.Own.Web.Site.The.Right.Way.Using.HTML .CSS .pdf

    This meant that the tutorials。ッ authors needed to resort to abusing various features of those browsers, such as using data tables to lay out pages. This certainly got many people building their ...

    Wrox.Beginning.ArcGIS.for.Desktop.Development.using.NET

    Whether you are an ArcGIS user with no background in programming or a programmer without experience with the ArcGIS platform, this book arms you with everything you need to get going with ArcGIS for ...

    AngularJS - Novice to Ninja.pdf.pdf )

    Adding Instance Functions and Properties to Controllers . . . . . . . . 35 Dependency Injection in Controllers With Minification . . . . . . . . . 37 Overview of Two-Way Data Binding . . . . . . . . ....

    unit3 What are you going to do练习题II2.doc

    本练习题的主题围绕“将要做什么”(What are you going to do),主要考察学生对于将来时态的运用以及询问他人计划的能力。在英语中,将来时态通常用"be going to"结构表示即将发生的动作或计划。下面我们将详细...

    Pro.Java.8.Programming.3rd.Edition.1484206428

    This book is for experienced Java programmers or developers looking to further refine or add to their skills and knowledge base. Table of Contents Chapter 1. Going Inside Java 8 Chapter 2. Designing ...

    Pro.Java.8.Programming.3rd.Edition.1484206428.epub

    This book is for experienced Java programmers or developers looking to further refine or add to their skills and knowledge base. Table of Contents Chapter 1. Going Inside Java 8 Chapter 2. Designing ...

    Commview V6.5

    CommView is a helpful tool for LAN administrators, security professionals, network programmers, or anyone who wants to have a full picture of the traffic going through one's PC or LAN segment....

    微软内部资料-SQL性能优化2

    A 32-bit process is normally limited to addressing 2 gigabytes (GB) of memory, or 3 GB if the system was booted using the /3G boot switch even if there is more physical memory available. By leveraging...

    be going to 结构用法精讲.docx

    "be going to"结构是英语中用来表达将来时态的一种方式,主要分为以下几个方面来讲解: 首先,"be going to"结构的基本形式是"主语 + be going to + 动词原形",这里的"be"动词会随着主语的人称和数发生变化,有am,...

    This Is Going to Hurt《疼痛难免(2022)》第一季第四集完整中英文对照剧本 .pdf

    This Is Going to Hurt《疼痛难免(2022)》第一季第四集完整中英文对照剧本 .pdf

    Preproduction.Blueprint.How.to.Plan.Game.Environments.and.Level.Designs.2nd.Ed

    All you need is a solid plan of what your environment is going to look and play like prior to creating it. "Preproduction Blueprint" is the planning system and workbook. These are the same steps I ...

    The Definitive Guide to AdonisJs_Building Node.js App with JavaScript-2018

    To get the most out of it, you should have a firm grasp of modern JavaScript and some knowledge of how to work with relational databases and the command line. I explain new and interesting bits of ...

    APPLIED NUMERICAL METHODS USING MATLAB.pdf

    know how to define the problems they want to solve using MATLAB and how to use the corresponding routines to solve their problems. We never deny that detailed knowledge about the algorithm (engine) of...

    where code and content meet

    I am not going to present one specific architecture, though. The reason for this is simple enough: websites are much too heterogeneous to be designed in a uniform way. Requirements differ a lot, as do...

    JSP Simple Examples

    In this example we are going to find out the factorial of 12 by using the while loop. In while loop the loop will run until the condition we have given gets true. Calculating factorial After going ...

    how to solve it

    George Polya revealed how the mathematical method of demonstrating a proof or finding an unknown can be of help in attacking any problem that can be "reasoned" out - from building a bridge to winning ...

    be-going-to的用法(讲义及答案).doc

    There is going to be a concert this weekend.(这个周末将有一场音乐会。) There are going to be many people at the park.(公园里将会有很多人。) 练习: 1. 这里明天将有一场足球比赛。 There _______ __...

    UE(官方下载)

    The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...

Global site tag (gtag.js) - Google Analytics