您好我正在尝试连接服务,但在调用
时失败并显示以下错误 CloseableHttpResponse response = httpclient.execute(httpget);
data = IOUtils.toString(response.getEntity().getContent(),
StandardCharsets.UTF_8);
我没有回复。因此,我假设这不可能是因为在读取整个数据之前连接已关闭?我怎么能在我的代码中处理这个?如何在生产中处理? 这是我的代码或服务的问题吗?
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:221) ~[?:1.8.0]
at java.net.SocketInputStream.read(SocketInputStream.java:152) ~[?:1.8.0]
at com.ibm.jsse2.b.a(b.java:240) ~[?:8.0 build_20171107]
at com.ibm.jsse2.b.a(b.java:142) ~[?:8.0 build_20171107]
at com.ibm.jsse2.at.a(at.java:630) ~[?:8.0 build_20171107]
at com.ibm.jsse2.at.a(at.java:893) ~[?:8.0 build_20171107]
at com.ibm.jsse2.f.read(f.java:60) ~[?:8.0 build_20171107]
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:139) ~[httpcore-4.4.4.jar:4.4.4]
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:155) ~[httpcore-4.4.4.jar:4.4.4]
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:284) ~[httpcore-4.4.4.jar:4.4.4]
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261) ~[httpcore-4.4.4.jar:4.4.4]
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165) ~[httpcore-4.4.4.jar:4.4.4]
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272) ~[httpcore-4.4.4.jar:4.4.4]
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124) ~[httpcore-4.4.4.jar:4.4.4]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) ~[httpclient-4.5.2.jar:4.5.2]
答案 0 :(得分:0)
似乎实体内容无法流式传输,因为它已关闭。 Connection reset
通常会告诉您GET请求/响应有问题。
但是,您可能还想查看此内容:
What's the recommended way to get the HTTP response as a String when using Apache's HTTP Client?
接受的答案显示了处理HTTP响应的好方法(使用EntityUtils
和HttpResponse
)。