检查网址是否存在时获取错误

时间:2011-04-24 17:23:34

标签: java url http-status-codes

我想在Java中使用以下代码持续监控网站是否处于活动状态:

URL url1 = new URL ("http://www.google.com");
HttpURLConnection code = (HttpURLConnection)url1.openConnection();
System.out.println(code.getResponseCode());

但我一直收到错误:

Exception in thread "main" java.net.UnknownHostException: www.google.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:158)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
    at java.net.Socket.connect(Socket.java:546)
    at java.net.Socket.connect(Socket.java:495)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:178)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
    at sun.net.www.http.HttpClient.New(HttpClient.java:321)
    at sun.net.www.http.HttpClient.New(HttpClient.java:338)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:935)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:876)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:801)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1139)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:397)
    at java.net.HttpURLConnection.getResponseMessage(HttpURLConnection.java:463)

我做错了吗?请指教。 感谢。

2 个答案:

答案 0 :(得分:1)

你是代理人吗?它可能需要您的代理设置才能正常工作..

答案 1 :(得分:0)

您的服务器无法解析google.com。检查您的DNS设置(resolv.conf和类似文件)。