gpt4 book ai didi

java - 线程 "main"java.net.ConnectException : Connection refused: connect when running Oracle tutorial example 中的异常

转载 作者:可可西里 更新时间:2023-11-01 16:28:24 30 4
gpt4 key购买 nike

我从 Oracle Docs 复制了示例程序,并在 Eclipse 中“按原样”运行它。

但是,每次运行它时我都会得到一个ConnectionException。我还需要配置什么吗?另外,我对这件事的理解很薄弱,能否请您推荐有关Java Web编程的初学者指南。

代码是

public class Test {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

我得到的异常(exception)是

Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.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 sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at Test.main(Test.java:9)

我可以通过 chrome 访问 http://www.oracle.com 并且可以访问互联网。如果是防火墙问题,谁能告诉我如何解决这个防火墙问题?

最佳答案

我已经获取了您的代码,并在我的 eclipse 环境中本地运行它。它工作并产生如下输出:

<!DOCTYPE html>

<html lang="en-US" class="no-js">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
var _U = "undefined";
var g_HttpRelativeWebRoot = "/ocom/";
var SSContributor = false;
var SSForceContributor = false;
var SSHideContributorUI = false;
var ssUrlPrefix = "/us/";
var ssUrlType = "2";

var g_navNode_Path = new Array();
g_navNode_Path[0] = '8';
var g_ssSourceNodeId = "8";
var g_ssSourceSiteId = "ocomen";
</script>
.....

这留下了一个问题,因为失败不是你程序的一部分,而是你环境的一部分。

当我输入这个答案时,您补充说您有代理设置。

您应该将 -Djava.net.useSystemProxies=true 添加到应用程序的运行框中,或者添加:

System.setProperty("java.net.useSystemProxies", "true");

在你的主要方法的第一行。这是 documented here :

java.net.useSystemProxies (default: false)

On recent Windows systems and on Gnome 2.x systems it is possible to tell the java.net stack, setting this property to true, to use the system proxy settings (both these systems let you set proxies globally through their user interface). Note that this property is checked only once at startup.

那应该引入系统代理设置....

有许多因素可能适用:

  1. 您的网络基础设施不允许您的程序直接连接到网络。许多位置使用代理设置来通过防火墙等。您的浏览器是否有代理?您可以将系统属性:java.net.useSystemProxies 设置为 true,并希望它会引入系统属性。
  2. 您的 Windows 个人防火墙通常会提示您让应用程序连接到网络。您是否拒绝/允许您的程序连接?
  3. 很多其他的东西....
  4. 可以

关于java - 线程 "main"java.net.ConnectException : Connection refused: connect when running Oracle tutorial example 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26150670/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com