开发者

Connection timed out. Why? [duplicate]

This question already has answers here: What is a connection timeout during a http request (2 answers) Closed last month.

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

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.SocksSocketIm开发者_JS百科pl.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)


We can't diagnose your networks for you. You need to do it yourself, or get your local admins to look at.

Things you should check before you bug your admins:

  • 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?

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.

If it is not a problem with your settings, you will need to get help from someone local who can help you diagnose the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜