连接超时.为什么? [英] Connection timed out. Why?

查看:21
本文介绍了连接超时.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码时出现异常.为什么?

I get an exception when I run this code. Why?

线程main"中的异常java.net.ConnectException:连接超时:connect

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 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 吗?
  • 你能用你的程序连接到任何东西吗?
  • can you ping the host?
  • can you connect to http://www.verisign.com using a web browser?
  • can you connect to https://www.verisign.com using a web browser?
  • can you connect to http://www.verisign.com using your program?
  • can you connect to anything using your program?

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

The chances are that your problem is firewall related. My first guess would be that you don't have the correct environment variables or Java system properties set to tell the JVM to use a local proxy server for outgoing HTTP / HTTPS requests.

这篇关于连接超时.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆