我正在尝试连接客户端服务器,在打开连接时遇到以下错误:
java.net.ConnectException: errno: 110 (Connection timed out), error:
Connection timed out (local port XXXXX to address 0:0:0:0:0:0:0:0, remote
port XXXX to address XXX.XXX.XXX.XX)
代码段:
URL url = new URL("http://XXX.XXX.XXX.XX:XXXX/services/ServiceEngine");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(json);
writer.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
答案 0 :(得分:0)
当服务器在预定时间段内无法响应并且与代码中的逻辑关系不大时,就会发生连接超时。
您可以使用HttpURLConnection.setConnectTimeout()更改此时间段,如下所示:
connection.setConnectTimeout(10000); //Time is set in milliseconds, so 1000 is 1 second.
正如另一位用户指出的那样,这可能是您的服务器尚未真正启动并准备好托管连接。