访问 URL 时出现 java.net.UnknownHostException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9909280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:51:07  来源:igfitidea点击:

java.net.UnknownHostException when accessing a URL

javaurlconnection

提问by blue-sky

Below code :

下面的代码:

URL oracle = new URL("http://www.oracle.com/");   
URLConnection inputStream =oracle.openConnection();
InputStream in = inputStream.getInputStream();

Throws this exception :

抛出这个异常:

java.net.UnknownHostException: www.oracle.com
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:234)
        at sun.net.www.http.HttpClient.New(HttpClient.java:307)
        at sun.net.www.http.HttpClient.New(HttpClient.java:324)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)

I think this exception is thrown because the proxy server is blocking my connection? I just want to parse some data from the URL, can I accomplish this without running into the firewall ?

我认为抛出这个异常是因为代理服务器阻止了我的连接?我只想从 URL 中解析一些数据,我可以在不进入防火墙的情况下完成此操作吗?

采纳答案by Philip Helger

If you are using a proxy server, please make sure that you are setting the appropriate system properties. See the Oracle page at http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.htmlfor details.

如果您使用代理服务器,请确保您设置了适当的系统属性。有关详细信息,请参阅位于http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html的 Oracle 页面。

回答by tony9099

Make sure to include the below line in your manifest. It should be a direct child of manifest node.

确保在清单中包含以下行。它应该是清单节点的直接子节点。

<uses-permission android:name="android.permission.INTERNET" />