导入P12证书

KeyStore clientStore = KeyStore.getInstance("PKCS12");
		clientStore.load(new FileInputStream("D://dev_env//jdk//jdk1.6.0_27//jre//szmsa.p12"), "12345678".toCharArray());
		KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		kmf.init(clientStore, "12345678".toCharArray());
		KeyManager[] kms = kmf.getKeyManagers();
		KeyStore trustStore = KeyStore.getInstance("JKS");
		trustStore.load(new FileInputStream("D://dev_env//jdk//jdk1.6.0_27//jre//cacerts"), "changeit".toCharArray());
		TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
		tmf.init(trustStore);
		TrustManager[] tms = tmf.getTrustManagers();
		SSLContext sslContext = null;
		sslContext = SSLContext.getInstance("TLS");
		sslContext.init(kms, tms, new SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
		URL url = new URL(TEST_URL);
		HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
		con.connect();
		InputStream inputStream = con.getInputStream();
		System.out.println(inputStream2String(inputStream));
		inputStream.close();
		con.disconnect();

 把Test_RUL改成IP

需要修改地方:

//		HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
		
		HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
		 con.setHostnameVerifier(new HostnameVerifier() {
				@Override
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});

 

你可能感兴趣的:(导入P12证书)