在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()上也观察到了同样的错误。
任何帮助都是非常感谢的。
发布于 2020-08-21 14:02:56
您试图以错误的顺序关闭请求和读取器。在我看来,你最好重新格式化代码,使用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;
}
https://stackoverflow.com/questions/63466356
复制相似问题