java - Getting time out exception while accessing through HttpsUrlConnection -
i trying access website(for e.g https://www.yahoo.com) through java code using eclipse ide, getting connection timeout exception.
i've checked eclipse able connect internet , same website coming fine in eclipse internal browser , in chrome.
the code given below
public static void main(string[] args) throws exception { sslcontext sslctx = sslcontext.getinstance("ssl"); sslctx.init(null, new x509trustmanager[] { new mytrustmanager() }, null); httpsurlconnection.setdefaultsslsocketfactory(sslctx.getsocketfactory()); httpsurlconnection.setdefaulthostnameverifier(new hostnameverifier() { @override public boolean verify(string hostname, sslsession session) { return true; } }); url url = new url("https://www.yahoo.com"); httpsurlconnection con = (httpsurlconnection) url.openconnection(); con.setrequestmethod("get"); con.connect(); //getting exception here if (con.getresponsecode() == httpsurlconnection.http_ok) { bufferedreader br = new bufferedreader(new inputstreamreader( con.getinputstream())); string line; while ((line = br.readline()) != null) { system.out.println(line); } br.close(); } else{ system.out.println("not able connect"); } con.disconnect();
}
mytrustmanager class implements x509trustmanager
class mytrustmanager implements x509trustmanager { public void checkclienttrusted(x509certificate[] chain, string authtype) { } public void checkservertrusted(x509certificate[] chain, string authtype) { } public x509certificate[] getacceptedissuers() { return new x509certificate[0]; }
}
after running code, application waiting few seconds , throwing following exception.
exception in thread "main" java.net.connectexception: connection timed out: connect @ java.net.plainsocketimpl.socketconnect(native method) @ java.net.plainsocketimpl.doconnect(unknown source) @ java.net.plainsocketimpl.connecttoaddress(unknown source) @ java.net.plainsocketimpl.connect(unknown source) @ java.net.sockssocketimpl.connect(unknown source) @ java.net.socket.connect(unknown source) @ com.sun.net.ssl.internal.ssl.sslsocketimpl.connect(unknown source) @ com.sun.net.ssl.internal.ssl.basesslsocketimpl.connect(unknown source) @ sun.net.networkclient.doconnect(unknown source) @ sun.net.www.http.httpclient.openserver(unknown source) @ sun.net.www.http.httpclient.openserver(unknown source) @ sun.net.www.protocol.https.httpsclient.<init>(unknown source) @ sun.net.www.protocol.https.httpsclient.new(unknown source) @ sun.net.www.protocol.https.abstractdelegatehttpsurlconnection.getnewhttpclient(unknown source) @ sun.net.www.protocol.http.httpurlconnection.plainconnect(unknown source) @ sun.net.www.protocol.https.abstractdelegatehttpsurlconnection.connect(unknown source) @ sun.net.www.protocol.https.httpsurlconnectionimpl.connect(unknown source) @ com.saf.server.test3.main(test3.java:31)
please me find out wrong!!!
Comments
Post a Comment