我寫了下面的代碼嘗試ping
。但是,當我運行它,但以下情況除外得到投擲:嘗試ping時,會引發java.net.UnknownHostException。我不明白原因
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();
}
}
}
爲什麼會這樣呢?服務器正在運行,頁面在Web瀏覽器中正常打開。
InetAddress.getByName(「host」)接受主機名而不接受他的協議。例如,如果你的主機是:「localhost:8084/server/abc/page.jsp」,這個工作 – carminePat