JAVA—HTTP客户端警告:Going to buffer response body of large or unknown size.
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JAVA—HTTP客户端警告:Going to buffer response body of large or unknown size.,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3458字,纯文字阅读大概需要5分钟。
内容图文
![JAVA—HTTP客户端警告:Going to buffer response body of large or unknown size.](/upload/InfoBanner/zyjiaocheng/601/cd7de556626c4eb7aba576ac3328342d.jpg)
关注微信公众号:CodingTechWork,一起学习进步。
原编码问题
??在修复原有代码bug时,发现日志里经常抛出Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended
,这种提示。查看了原有代码中的逻辑如下:
... ...
HttpClient httpclient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
int statusCode = httpclient.executeMethod(getMethod);
String respContent = getMethod.getResponseBodyAsString();
... ...
??原因就在于使用了getResponseBodyAsString()
方法
源码分析
getResponseBodyAsString()源码
//getResponseBodyAsString()方法源码
public String getResponseBodyAsString() throws IOException {
byte[] rawdata = null;
if (this.responseAvailable()) {
//调用了getResponseBody(),容易消耗内存
rawdata = this.getResponseBody();
}
return rawdata != null ? EncodingUtil.getString(rawdata, this.getResponseCharSet()) : null;
}
//responseAvailable()方法源码
private boolean responseAvailable() {
return this.responseBody != null || this.responseStream != null;
}
//getResponseBody()方法源码
public byte[] getResponseBody() throws IOException {
if (this.responseBody == null) {
InputStream instream = this.getResponseBodyAsStream();
if (instream != null) {
long contentLength = this.getResponseContentLength();
if (contentLength > 2147483647L) {
throw new IOException("Content too large to be buffered: " + contentLength + " bytes");
}
int limit = this.getParams().getIntParameter("http.method.response.buffer.warnlimit", 1048576);
if (contentLength == -1L || contentLength > (long)limit) {
//这里是warn的原文
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) {
outstream.write(buffer, 0, len);
}
outstream.close();
this.setResponseStream((InputStream)null);
this.responseBody = outstream.toByteArray();
}
}
return this.responseBody;
}
??从源码中可以看出,warn的条件是(contentLength == -1L || contentLength > (long)limit)
,如果http头没有指定contentLength
或大于上限值(默认1M),就会抛异常。其实,如果返回的结果比较确定,对程序没有太大影响。而对于返回结果不确定时,源码也建议我们使用下面的getResponseBodyAsStream()
方法。
getResponseBodyAsStream()源码
public InputStream getResponseBodyAsStream() throws IOException {
if (this.responseStream != null) {
return this.responseStream;
} else if (this.responseBody != null) {
InputStream byteResponseStream = new ByteArrayInputStream(this.responseBody);
LOG.debug("re-creating response stream from byte array");
return byteResponseStream;
} else {
return null;
}
}
??从源码中可以看出,getResponseBodyAsStream()
内部没有使用getResponseBody()
方法,避免了内存耗尽问题,而是使用了InputStream
流方式处理。
优化原编码
... ...
HttpClient httpclient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
int statusCode = httpclient.executeMethod(getMethod);
//String respContent = getMethod.getResponseBodyAsString();
//使用getResponseBodyAsStream()
InputStream inputStream = getMethod.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
LOGGER.info("respContent: {}", stringBuffer.toString());
... ...
??测了一把,哦豁,不再报这个异常了。完美。
内容总结
以上是互联网集市为您收集整理的JAVA—HTTP客户端警告:Going to buffer response body of large or unknown size.全部内容,希望文章能够帮你解决JAVA—HTTP客户端警告:Going to buffer response body of large or unknown size.所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。