【问题标题】:How to release connection with Apache http client property?如何释放与 Apache http 客户端属性的连接?
【发布时间】:2012-05-19 01:54:39
【问题描述】:

我使用 Apache http 客户端 4.1,并且总是在 HttpGet 执行后关闭响应 InputStream(例如通过调用 EntityUtils.consume(response.getEntity))。

现在我想知道如果HttpGet 执行抛出异常,连接是否被正确释放。在这种情况下我应该明确释放连接吗?

【问题讨论】:

    标签: java apache-httpclient-4.x


    【解决方案1】:

    如果您获得了 finally 子句中的 I/O 对象,请始终关闭它们。

    InputStrem input = acquireStream( );
    
    try
    {
      useStream( input );
    }
    finally
    {
      input.close( );
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 2019-08-17
      • 1970-01-01
      • 2017-05-04
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      相关资源
      最近更新 更多