【发布时间】:2023-03-21 00:01:02
【问题描述】:
我编写了以下代码来尝试ping。但是当我运行它时,会抛出以下异常:
java.net.UnknownHostException: http://localhost:8084/server/index.jsp
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getByName(Unknown Source)
at Tester.main(Tester.java:10)
import java.net.InetAddress;
class Tester {
public static void main(String args[]) {
try {
InetAddress address = InetAddress.getByName("http://localhost:8084/server/index.jsp");
boolean isReachable = address.isReachable(2000);
if(isReachable)
System.out.println("The address is reachable");
else
System.out.println("The address is not reachable");
} catch(Exception exc) {
exc.printStackTrace();
}
}
}
为什么会这样?服务器正在运行,页面在网络浏览器中打开正常。
【问题讨论】:
-
InetAddress.getByName("host") 接受主机名而不是他的协议。例如,如果您的主机是:“localhost:8084/server/abc/page.jsp”这有效
标签: java networking ping unknown-host