private static String sendGetHttps(String url)
{
BufferedReader in = null;
try{
logger.info("Connecting to url: " + url);
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
logger.info("Response Code after connecting to url: " + responseCode);
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
logger.info("Response String: " + response.toString());
return response.toString();
}catch(Exception e ){
throw new PMRException(e.getMessage(),e);
}
finally
{
if(in!= null)
{
try
{
in.close();
}
catch(Exception e)
{
}
}
}
}
public static void getSSLCertificate(String keyStoreLocation, String keyStorePassword, String jksPassword) {
final char[] JKS_PASSWORD = jksPassword.toCharArray();
final char[] KEY_PASSWORD = keyStorePassword.toCharArray();
try {
// Get the JKS contents
final KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(new File(keyStoreLocation)), JKS_PASSWORD);
final KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, KEY_PASSWORD);
final TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
// Creates a socket factory for HttpsURLConnection using JKS contents
final SSLContext sc = SSLContext.getInstance("TLS");
sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom());
final SSLSocketFactory socketFactory = sc.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);
} catch (IOException exc) {
throw new RuntimeException(exc);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source)
public class ProgramTest {
public static void main(String[] args){
try {
NetUtils.getSSLCertificate("c:/Users/kingw/.keystore", "changeit", "changeit");
NetUtils.sendGet("https://testing.dev.cfed.local/api/auth/" + "c076utf5-2aaa-4b58-92d3-981dyhfefvgyyh" + "/" + "22");
}catch(Exception e){
e.printStackTrace();
}
}
}
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.