【问题标题】:javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify with java 11javax.net.ssl.SSLException:在使用 java 11 接收对等方的 close_notify 之前关闭入站
【发布时间】:2020-08-18 10:05:49
【问题描述】:

我在 java 11 上的 http 响应上调用 close 时遇到以下异常。这曾经与 java 8 一起使用。

Caused by: javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:133)
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:313)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:269)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:260)
    at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:737)
    at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:716)
    at org.apache.http.impl.BHttpConnectionBase.close(BHttpConnectionBase.java:327)
    at org.apache.http.impl.conn.LoggingManagedHttpClientConnection.close(LoggingManagedHttpClientConnection.java:81)
    at org.apache.http.impl.conn.CPoolEntry.closeConnection(CPoolEntry.java:70)
    at org.apache.http.impl.conn.CPoolProxy.close(CPoolProxy.java:86)
    at org.apache.http.impl.execchain.ConnectionHolder.releaseConnection(ConnectionHolder.java:103)
    at org.apache.http.impl.execchain.ConnectionHolder.close(ConnectionHolder.java:156)
    at org.apache.http.impl.execchain.HttpResponseProxy.close(HttpResponseProxy.java:62)

上述异常发生在以下代码中调用 response.close() 时:

    HttpGet httpRequest = new HttpGet(url);
    CloseableHttpResponse response = null;
    BufferedReader reader = null;
    try {
        response = httpClient.execute(httpRequest);
        reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while((line = reader.readLine()) != null) {
         // do something with the line
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (response != null) {
            response.close();
        }
        if (reader != null) {
            reader.close();
        }
    }

我正在使用 httpclient 4.5.3。 我在 reader.close() 上也观察到了同样的错误。

感谢任何帮助。

【问题讨论】:

  • 确实是关闭顺序错误的问题。先关闭阅读器,然后连接,就可以了。

标签: java-11 apache-httpcomponents


【解决方案1】:

您试图以错误的顺序关闭请求和阅读器。在我看来,您最好重新格式化代码以使用自动关闭资源的 try-with-resource 块,这样您就再也不会遇到这个问题了:

HttpGet httpRequest = new HttpGet(url);
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) {
        String line = "";
        while ((line = reader.readLine()) != null) {
            // do something with the line
        }
    }
} catch (Exception e) {
    e.printStackTrace();
    throw e;
}

【讨论】:

    猜你喜欢
    • 2019-04-07
    • 2019-05-17
    • 2019-03-27
    • 2019-09-25
    • 2021-10-25
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    相关资源
    最近更新 更多