Java网络套接字异常-连接重置-如何从Java修复

时间:2018-12-28 09:58:04

标签: java sockets httpurlconnection

我尝试执行以下代码段:

URL obj = new URL(serviceLastURL);
conn = (HttpURLConnection) obj.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("x-ibm-client-id", IrProperties.getXIBMClientId());
conn.setRequestProperty("x-ibm-client-secret", IrProperties.getXIBMClientSecret());
conn.setRequestProperty("OAuth-Token", accessToken); conn.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;)
    sb.append((char) c);

一旦连接突然失败, 当我尝试连接到同一URL时,出现以下异常:

java.net.socketException - connection reset

如何通过响应将URL重新刷新?

如何防止这种异常?

1 个答案:

答案 0 :(得分:0)

您没有关闭连接。使用后请务必关闭连接。

此外,您的请求连接可能无法从客户端获得。