http组件&x27;s ssl连接导致套接字关闭

http组件&x27;s ssl连接导致套接字关闭,ssl,apache-httpclient-4.x,apache-httpcomponents,Ssl,Apache Httpclient 4.x,Apache Httpcomponents,我正在尝试从Web服务器获取一些数据,它可以很好地与http配合使用 但是,当我尝试https(ssl连接)时,会出现如下异常 我得到了正确的http状态码200和响应内容长度2230 java.net.SocketException: Socket is closed at sun.security.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1483) at sun.securit

我正在尝试从Web服务器获取一些数据,它可以很好地与http配合使用

但是,当我尝试https(ssl连接)时,会出现如下异常

我得到了正确的http状态码200和响应内容长度2230

        java.net.SocketException: Socket is closed
            at sun.security.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1483)
            at sun.security.ssl.AppInputStream.read(AppInputStream.java:92)
            at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)
            at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)
            at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:183)
            at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:144)
            at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:121)
我的代码如下所示,使用ApacheHttpComponents httpclient(4.2.5)库


基本上,答案是@Avner

(对我来说)问题是,在读取实体之前,响应已关闭

我这样做是错误的:

HttpEntity entity = null;
try (CloseableHttpResponse response = client.execute(request)) {
     entity = response.getEntity();
}
read(entity);
try (CloseableHttpResponse response = client.execute(request)) {
     HttpEntity  entity = response.getEntity();
     read(entity);
}
以下工作正常

HttpEntity entity = null;
try (CloseableHttpResponse response = client.execute(request)) {
     entity = response.getEntity();
}
read(entity);
try (CloseableHttpResponse response = client.execute(request)) {
     HttpEntity  entity = response.getEntity();
     read(entity);
}

可能不太明显的部分:第一个示例中的块在读取流之前关闭了流。

我知道已经有一段时间了,但是您是否得到过这个问题的答案…?我在读取实体之前调用response.close()时出错。