View Single Post
  #1 (permalink)  
Old 07-03-2007, 05:38 PM
Albert Albert is offline
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
java.net.ConnectException: Connection refused
Recently I had been struggling with some code to get a https connection to work.

It was coming up with an error that no trusted certificates could be found. I'm working on the program on Windows and putting into a unix server to be run. This worked fine initially with no problems, then the server changed from http to https, so I had to implement some changes to deal with this, now however, even though it works fine on windows when put on unix,
it throws up this error:

java.net.ConnectException: Connection refused

Code:
java.security.Security.insertProviderAt(new sun.security.provider.Sun(),2); java.security.Security.addProvider(new sun.security.provider.Sun()); java.security.Security.insertProviderAt(new com.sun.net.ssl.internal.ssl.Provider(),1); // set protocol to java.protocol.handler.pkgs property to 'HTTP' // -- to do with if report server site is HTTPS System.setProperty("java.protocol.handler.pkgs","HTTP"); // Trustmanager -- set to trust any certificate for a secure server TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) { } } }; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } URL url = null; try { url = new URL( rptUrl ); //rptUrl contains url to use } catch (MalformedURLException mue) { mue.printStackTrace(); } //HttpsURLConnection c; try { URLConnection c = url.openConnection(); //set cache and request method settings c.setUseCaches(false); //set other headers c.setRequestProperty ("Content-Type", "application/pdf"); //connect to the server.. c.connect(); ////some code about what to do with file etc etc....
Any ideas? Thanks.

Albert
Reply With Quote
Sponsored Links