WARN警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended

 

 

使用Apache HttpClient发送请求,有大量WARN警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended

 

原因:使用了 getResponseBodyAsString()的缘故

修改:getResponseBodyAsStream(),如下:

public Response get(String url, NameValuePair[] params) {
	Response response = new Response();

	GetMethod method = new GetMethod(url);
	method.setQueryString(params);
	method.getParams().setContentCharset(UTF8);

	try {
		response.setStatusCode(httpClient.executeMethod(method));
 
	   // 使用getResponseBodyAsStream()代替getResponseBodyAsString使用流的方式来接收
		// response.setBody(method.getResponseBodyAsString());
		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);
		}
		response.setBody(stringBuffer.toString());
		return response;
	} catch (Exception e) {
		e.getStackTrace();
	} finally {
		method.releaseConnection();
	}
	return null;
}

  

  

posted @ 2019-05-23 19:54  裸奔的太阳  阅读(5657)  评论(0编辑  收藏  举报