未知主机异常

发布于 2024-11-09 13:34:02 字数 1623 浏览 0 评论 0原文

try { 
{
    long startTime = System.currentTimeMillis();

        String source="s";
        String source1="s";
        URL google = new URL("http://google.com/"); 
        HttpURLConnection yc =(HttpURLConnection)google.openConnection(); 
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); 
        String inputLine;


        while ((inputLine = in.readLine()) != null) { 
            source=source.concat(inputLine);
        }
        in.close();

        yc.disconnect();

    }

long endTime1 = System.currentTimeMillis();
System.out.println("Total elapsed time in execution of method callMethod() is :"+ (endTime1-startTime));


    } 
}

当我通过命令提示符尝试上述操作时 我

java.net.UnknownHostException: google.com 
at java.net.PlainSocketImpl.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at sun.net.NetworkClient.doConnect(Unknown Source) 
at sun.net.www.http.HttpClient.openServer(Unknown Source) 
at sun.net.www.http.HttpClient.openServer(Unknown Source) 
at sun.net.www.http.HttpClient.<init>(Unknown Source) 
at sun.net.www.http.HttpClient.New(Unknown Source) 
at sun.net.www.http.HttpClient.New(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) 
at ScagntJavaHttp.httpMakeRequest(ScagntJavaHttp.java:185) 
at test.main(test.java:23)

有谁能帮我解决这个问题吗?

try { 
{
    long startTime = System.currentTimeMillis();

        String source="s";
        String source1="s";
        URL google = new URL("http://google.com/"); 
        HttpURLConnection yc =(HttpURLConnection)google.openConnection(); 
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); 
        String inputLine;


        while ((inputLine = in.readLine()) != null) { 
            source=source.concat(inputLine);
        }
        in.close();

        yc.disconnect();

    }

long endTime1 = System.currentTimeMillis();
System.out.println("Total elapsed time in execution of method callMethod() is :"+ (endTime1-startTime));


    } 
}

when i tried the above through command prompt
i got

java.net.UnknownHostException: google.com 
at java.net.PlainSocketImpl.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at sun.net.NetworkClient.doConnect(Unknown Source) 
at sun.net.www.http.HttpClient.openServer(Unknown Source) 
at sun.net.www.http.HttpClient.openServer(Unknown Source) 
at sun.net.www.http.HttpClient.<init>(Unknown Source) 
at sun.net.www.http.HttpClient.New(Unknown Source) 
at sun.net.www.http.HttpClient.New(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) 
at ScagntJavaHttp.httpMakeRequest(ScagntJavaHttp.java:185) 
at test.main(test.java:23)

Can any help me in resolving this one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ら栖息 2024-11-16 13:34:02

我相信这是一个代理问题。
尝试查看您的浏览器中是否有代理定义,然后进行设置:

    ProxySelector.setDefault(new ProxySelector() {

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            throw new RuntimeException("Proxy connect failed", ioe);
        }

        @Override
        public List select(URI uri) {
            return Arrays
                .asList(new Proxy(Proxy.Type.HTTP,
                                  new InetSocketAddress(proxyHost,
                                                        proxyPort)));
        }
    });

要查看 IE 中是否有代理定义,请转到“工具”-“Internet 选项”--“连接”--“Lan 设置”

I believe it's a proxy problem.
Try to see if you have a proxy definition in your browser and then set it:

    ProxySelector.setDefault(new ProxySelector() {

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
            throw new RuntimeException("Proxy connect failed", ioe);
        }

        @Override
        public List select(URI uri) {
            return Arrays
                .asList(new Proxy(Proxy.Type.HTTP,
                                  new InetSocketAddress(proxyHost,
                                                        proxyPort)));
        }
    });

To see if you have proxy definition in IE, go to Tools - Internet Options -- Connections -- Lan Settings

我喜欢麦丽素 2024-11-16 13:34:02

当您收到 java.net.UnknownHostException 时,尝试从您的主机网址中删除 http:// 并检查您的互联网连接和主机是否存在(对于 google 来说可能是安全的......)

Try removing http:// from your host url when you get java.net.UnknownHostException and check your internet connection and the host exists (probably safe with google . . .)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文