Issue
I am getting the following exception on calling close on the http response on java 11. This used to work with 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)
The above exception happens on calling response.close() in the below code:
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();
}
}
I am using httpclient 4.5.3. I observe the same error on reader.close() as well.
Any help is appreciated.
Solution
You are trying to close the request and the reader in the wrong order. In my opinion you should better reformat the code to use try-with-resource blocks that automatically close the resource so that you can never again run into this problem:
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;
}
Answered By - Robert
Answer Checked By - Mildred Charles (JavaFixing Admin)