javax.net.ssl.SSLException:在使用Java 11接收对等方的close_notify之前关闭入站

问题描述

在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()上也观察到相同的错误。

感谢您的帮助。

解决方法

您正在尝试以错误的顺序关闭请求和阅读器。我认为您最好将代码重新格式化为使用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;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...