在 使用
getResponseBodyAsString() 一直提示:
Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
<span style="font-size:18px;">public byte[] getResponseBody()
throws IOException
{
if (this.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 > 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 ? (int)contentLength : 4096);
byte[] buffer = new byte[4096];
int len;
while ((len = instream.read(buffer)) > 0)
{
int i;
outstream.write(buffer, 0, i);
}
outstream.close();
setResponseStream(null);
this.responseBody = outstream.toByteArray();
}
}
return this.responseBody;
}
//--------------------
public long getResponseContentLength()
{
Header[] headers = getResponseHeaderGroup().getHeaders("Content-Length");
if (headers.length == 0) {
return -1L;
}
if (headers.length > 1) {
LOG.warn("Multiple content-length headers detected");
}
for (int i = headers.length - 1; i >= 0; ) {
Header header = headers[i];
try {
return Long.parseLong(header.getValue());
} catch (NumberFormatException e) {
if (LOG.isWarnEnabled())
LOG.warn("Invalid content-length value: " + e.getMessage());
i--;
}
}
return -1L;
}</span>
如果返回的头信息没有指定长度或长度大于1M则抛出如上异常。
则需把 getResponseBodyAsString()换成 getResponseBodyAsStream()即可
<span style="font-size:18px;">InputStream inputStream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String str= "";
while((str = br.readLine()) != null){
stringBuffer .append(str );
}</span>