SSL connection with Java

I am trying to make a test SSL connection using the following Java code:

String httpsURL = "https://www.somehost.com";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();

InputStream ins = con.getInputStream();
InputStreamReader isr=new InputStreamReader(ins);
BufferedReader in =new BufferedReader(isr);

String inputLine;

while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);

in.close();

      

When I connect to Host A everything works fine - the connection is made and the response is received.

However, when I connect to Host B, which is protected by a certificate that is issued by the same authority as Host A, I get the following exception:

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

      

Everything I've read so far says that I need to install the certificates in the keystore, however if this is the solution, then why is host A running while host B is down?

How probably useless - if I write a similar piece of C # code, then the connection is successfully negotiated for both hosts A and B - the same applies for URL navigation in the browser.

+2


a source to share


2 answers


The most likely causes are

  • Host B uses a self-signed certificate.
  • The certificate is signed by a CA that is not in your trust store.
  • The certificate is signed with an intermediate certificate, but Host B is misconfigured so it does not send a server certificate with an intermediate certificate.


For # 1, # 2, you need to import the CA certificate or certificate into the trust store.

For # 3, tell Host B to submit the intermediate certificate.

+2


a source


This may be because you do not have a valid certificate path or perhaps because you do not have a CA certificate for any company to verify.



-1


a source







All Articles