Connection timed out

  • Follow


I want to test this code which establishes SSL connection with theserver. Why it throws the exception as below. Also, how to retrievethe content of the desired page on that host?:import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.security.Principal;import java.security.cert.X509Certificate;import javax.net.ssl.SSLPeerUnverifiedException;import javax.net.ssl.SSLSession;import javax.net.ssl.SSLSocket;import javax.net.ssl.SSLSocketFactory;public class MainClass {  public static void main(String[] args) throws Exception {    SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();    String hostName = "website.com";    String fileName = "";    SSLSocket sslsock = (SSLSocket) factory.createSocket(hostName,443);    SSLSession session = sslsock.getSession();    X509Certificate cert;    try {      cert = (X509Certificate) session.getPeerCertificates()[0];    } catch (SSLPeerUnverifiedException e) {      System.err.println(session.getPeerHost() + " did not present avalid certificate.");      return;    }    System.out.println(session.getPeerHost() + " has presented acertificate belonging to:");    Principal p = cert.getSubjectDN();    System.out.println("\t[" + p.getName() + "]");    System.out.println("The certificate bears the valid signatureof:");    System.out.println("\t[" + cert.getIssuerDN().getName() + "]");    System.out.print("Do you trust this certificate (y/n)? ");    System.out.flush();    BufferedReader console = new BufferedReader(newInputStreamReader(System.in));    if (Character.toLowerCase(console.readLine().charAt(0)) != 'y')      return;    PrintWriter out = new PrintWriter(sslsock.getOutputStream());    out.print("GET " + fileName + " HTTP/1.0\r\n\r\n");    out.flush();    BufferedReader in = new BufferedReader(newInputStreamReader(sslsock.getInputStream()));    String line;    while ((line = in.readLine()) != null)      System.out.println(line);    sslsock.close();  }}Exception in thread "main" java.net.ConnectException: Connection timedout: 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 com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.<init>(Unknown Source)	atcom.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl.createSocket(UnknownSource)	at SSL.SSLTest.main(SSLTest.java:20)
0
Reply vunet.us (106) 10/2/2007 9:00:00 PM


0 Replies
260 Views

(page loaded in 0.044 seconds)

Similiar Articles:













7/26/2012 1:14:35 PM


Reply: