gpt4 book ai didi

java - 连接超时。为什么?

转载 作者:搜寻专家 更新时间:2023-11-01 02:16:11 25 4
gpt4 key购买 nike

当我运行这段代码时出现异常。为什么?

Exception in thread "main" java.net.ConnectException: Connection timed out: connect

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class MainClass {

public static void main(String[] args) throws Exception {
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

URL url = new URL("https://www.verisign.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}

异常(exception):

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:204)
at java.net.URL.openStream(URL.java:1010)
at https.ssl.MainClass.main(MainClass.java:13)

最佳答案

我们无法为您诊断您的网络。您需要自己动手,或者让您本地的管理员查看。

打扰您的管理员之前应该检查的事项:

  • 你能ping主机吗?
  • 您可以使用网络浏览器连接到 http://www.verisign.com 吗?
  • 您可以使用网络浏览器连接到 https://www.verisign.com 吗?
  • 您可以使用您的程序连接到 http://www.verisign.com 吗?
  • 你能用你的程序连接到任何东西吗?

很可能您的问题与防火墙有关。我的第一个猜测是您没有设置正确的环境变量或 Java 系统属性来告诉 JVM 使用本地代理服务器来处理传出的 HTTP/HTTPS 请求。

如果不是您的设置问题,您需要向本地人寻求帮助,他们可以帮助您诊断问题。

关于java - 连接超时。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5907037/

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