HttpUrlConnection java.net.SocketException: Software caused connection abort: recv failed

时间:2023-03-08 19:44:17

最近做java swing程序在模拟httprequest请求的时候出现了这个错误

java.net.SocketException: Software caused connection abort: recv failed

显示是在connection在获得con.getInputStream()时随机出现这个exception,

最后我感觉是 16行把输出流关闭了,这个时候server会认为连接已断开,于是该把16行放到27行,不知道对不对。

      HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(requestMethod);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(true);
con.setRequestProperty("Content-Type", contentType);
con.setRequestProperty("Accept-Charset", charset); DataOutputStream printout = new DataOutputStream(con.getOutputStream()); // This is the POST
// String content = "type=ask_bid_list_table&symbol="+coinName+"_cny"; printout.writeBytes(content);
printout.flush();
printout.close();
if (con.getResponseCode() == 200) {
BufferedReader input = new BufferedReader(new InputStreamReader(
con.getInputStream(), charset));
String str;
StringBuilder sb = new StringBuilder();
// Read the response while (null != ((str = input.readLine()))) {
sb.append(str);
} input.close();
        }

这个错误只是随机出现。