[Resolvido] PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Amigos, estou obtendo a seguinte exception, durante uma tentativa de post: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Eu criei um fromulário.html e realizei o post para a url e funcionou, mas quando executo o código abaixo não dá certo rs

String url = "https://XXXXXXXXXXXXXX";
		URL obj = new URL(url);
		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

		//add reuqest header
		con.setRequestMethod("POST");
		con.setRequestProperty("User-Agent", "USER_AGENT");
		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

		String urlParameters = "SAMLResponse=XXXXXXXXXX";
		
		// Send post request
		con.setDoOutput(true);
		DataOutputStream wr = new DataOutputStream(con.getOutputStream());
		wr.writeBytes(urlParameters);
		wr.flush();
		wr.close();

		int responseCode = con.getResponseCode();
		System.out.println("\nSending 'POST' request to URL : " + url);
		System.out.println("Post parameters : " + urlParameters);
		System.out.println("Response Code : " + responseCode);

		BufferedReader in = new BufferedReader(
		        new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuffer response = new StringBuffer();

		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();
		

Eu resolvi do seguinte modo:

keytool -import -alias example -keystore “C:\Program Files\Java\jdk1.8.0_201\jre\lib\security\cacerts” -file

Basta baixar o certificado e rodar o comando acima.

1 curtida