SSL exception in JAVA6 but no in JAVA8

218 Views Asked by At

Hy

I try to connect a java program to an REST API.

With the same part of code I have a Java Exception in Java 6 and it works fine in Java 8.

It's the same environment :

  • trustore
  • machine
  • unix user

the code :

import java.io.DataInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainClass {

    public static void main (String[] args){

        String serviceUrl = "https://api.domain.com" + "/endpont/path";
        try {
            URL url = new URL(serviceUrl);
            URLConnection connection = null;
            try{
                connection = url.openConnection();
                connection.setRequestProperty("Accept", "application/json");
                connection.setRequestProperty("Content-Type", "application/json");
                String body = "";
                String inputLine;
                DataInputStream input = new DataInputStream (connection.getInputStream());
                while (((inputLine = input.readLine()) != null)){
                    body += inputLine;
                }
                System.out.println(body);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

the error in Java 6 : sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Somebody know how it's different ? Can I use some tricks to have the same result in Java 6 ?

The CN of the cert is a wildcard : "*.domain.com" . It can be the cause ?

I tried several api but there all used the sun SSL layer. Do you know an other to replace it ?

1

There are 1 best solutions below

2
On

JRE has it's own keystore, where certificates can be stored. Maybe your JDK/JRE for Java 6 has different keys than Java 8.