Httpclient 为什么BasicResponseHandler在成功获取响应字符串后不使用响应实体
我最近遇到了一个ConnectionPoolTimeOutException,我想知道它是否与我的响应处理程序有关。如果响应实体是资源,并且应该在不需要时立即释放,为什么Apache BasicResponseHandler在返回响应字符串之前不使用该实体Httpclient 为什么BasicResponseHandler在成功获取响应字符串后不使用响应实体,httpclient,httpresponse,apache-httpclient-4.x,Httpclient,Httpresponse,Apache Httpclient 4.x,我最近遇到了一个ConnectionPoolTimeOutException,我想知道它是否与我的响应处理程序有关。如果响应实体是资源,并且应该在不需要时立即释放,为什么Apache BasicResponseHandler在返回响应字符串之前不使用该实体 @Immutable public class BasicResponseHandler implements ResponseHandler<String> { /** * Returns the respo
@Immutable
public class BasicResponseHandler implements ResponseHandler<String> {
/**
* Returns the response body as a String if the response was successful (a
* 2xx status code). If no response body exists, this returns null. If the
* response was unsuccessful (>= 300 status code), throws an
* {@link HttpResponseException}.
*/
public String handleResponse(final HttpResponse response)
throws HttpResponseException, IOException {
StatusLine statusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() >= 300) {
EntityUtils.consume(entity);
throw new HttpResponseException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
//Why not this:
//EntityUtils.consume(entity);
//String responseStr = entity == null ? null : EntityUtils.toString(entity);
//return responseStr;
return entity == null ? null : EntityUtils.toString(entity);
}
}
因为它是由 如果查看这两个方法,它们都会关闭Inputstream,如下所示
public final class EntityUtils {
public static void consume(HttpEntity entity) throws IOException {
// ..
InputStream instream = entity.getContent();
// ..
instream.close();
//..
}
public static String toString(HttpEntity entity, String defaultCharset) throws IOException, ParseException {
// ..
InputStream instream = entity.getContent();
// ..
instream.close();
return var9;
}
}
public final class EntityUtils {
public static void consume(HttpEntity entity) throws IOException {
// ..
InputStream instream = entity.getContent();
// ..
instream.close();
//..
}
public static String toString(HttpEntity entity, String defaultCharset) throws IOException, ParseException {
// ..
InputStream instream = entity.getContent();
// ..
instream.close();
return var9;
}
}