1. 程式人生 > >異常:InetAddress的UnknowHostException

異常:InetAddress的UnknowHostException

呼叫程式碼如下

System.out.print(InetAddress.getByName("http://www.baidu.com").toString());

然後控制檯輸出如下

objc[94133]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined. java.net.UnknownHostException:

http://www.baidu.com: unknown error at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) at java.net.InetAddress.getAllByName0(InetAddress.java:1276) at java.net.InetAddress.getAllByName(InetAddress.java:1192) at java.net.InetAddress.getAllByName(InetAddress.java:1126) at java.net.InetAddress.getByName(InetAddress.java:1076) at com.shapiya.request.core.utils.http.Request.main(Request.java:222)

Process finished with exit code 0

原因 各種除錯,發現我們給域名的時候不能帶協議!!!也就是說上面的程式碼改成

System.out.print(InetAddress.getByName("www.baidu.com").toString());

說句實在話,這篇部落格我都不好意思寫!

(完)