【发布时间】:2017-11-12 17:35:29
【问题描述】:
我正在尝试从我也可以访问的特定 URL 读取内容。我遇到了问题。所以我只是从here的基本示例开始
我正面临 ConnectionException。
public class UrlReader {
public static void main(String[] args) throws Exception {
System.setProperty("http.proxyHost","http://www.oracle.com/");
System.setProperty("http.proxyPort", "8081");
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("doaminName\\username","password".toCharArray());
}
});
URL url = new URL("http://www.oracle.com/");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
// Read it ...
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
/* URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();*/
}
}
例外:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
我已经按照一些在线链接来解决它,但没有成功。 是否有人在使用 工作笔记本电脑 工作时遇到过此类问题。我想连接到某个 URL 并使用 java 从该 URL 读取内容。
【问题讨论】:
-
你试过
https://www.oracle.com/吗?该页面是 https。 -
是的,线程“主”java.net.ConnectException 中的相同异常异常:连接被拒绝:连接。这是因为我正在尝试使用可能有防火墙的工作笔记本电脑(不确定)。 @GlenPierce
-
www.oracle.com 不是 HTTP 代理主机,www.oracle.com:8081 不是 HTTP 代理端口。尝试使用自己作为代理连接到主机没有任何意义。 “连接被拒绝”并不意味着“无法读取数据”。你的问题没有意义。
标签: java inputstream java-io