如何解决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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。