我试图检查是否可以使用"isReachable"方法访问某些主机.
line 113: oaiBaseURL = "http://www.cnn.com";//////////////////////////////////////
line 114: boolean res = InetAddress.getByName(oaiBaseURL).isReachable(10000);
line 115: System.out.println("------reachable:"+res);
Run Code Online (Sandbox Code Playgroud)
并获取以下错误消息(在eclipse中):
java.net.UnknownHostException: http://www.cnn.com
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.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getByName(Unknown Source)
at com.irWizard.web.validator.WizardValidator.validateForm(WizardValidator.java:114)
Run Code Online (Sandbox Code Playgroud)
有谁知道这个错误可能是什么原因?
提前致谢!
您需要删除http://前缀.
据我所知,该InetAddress.getByName()方法采用主机名而不是URL.
您可以按如下方式更改代码:
URL url = new URL("http://www.cnn.com");
boolean res = InetAddress.getByName(url.getHost()).isReachable(10000);
System.out.println("------reachable:"+res);
Run Code Online (Sandbox Code Playgroud)
但请记住该方法isReachable()用于确定其是否可访问的机制.它主要使用ICMP技术,许多网站或中间防火墙可能阻止这些技术.
| 归档时间: |
|
| 查看次数: |
4802 次 |
| 最近记录: |