Current location - Quotes Website - Signature design - How does a java client access an https website (server) with a pfx format certificate?
How does a java client access an https website (server) with a pfx format certificate?

Use HttpURLConnection? to access the https address.

The following is how to import the JKS certificate for your reference.

String?keystorefile?=?"file";

String?keystorepw?=?"password";

String?keypw?=?"password";

KeyStore?keystore?=?KeyStore.getInstance("JKS");

keystore.load(?new?FileInputStream(keystorefile),?keystorepw.toCharArray());

KeyManagerFactory?keymanagerfactory?=?KeyManagerFactory.getInstance("SunX509");

keymanagerfactory.init(keystore,?keypw.toCharArray());

KeyManager?akeymanager[]?= ?keymanagerfactory.getKeyManagers();

TrustManagerFactory?trustmanagerfactory?=?TrustManagerFactory.getInstance("SunX509");

trustmanagerfactory.init(keystore);

TrustManager?atrustmanager[]?=?trustmanagerfactory.getTrustManagers();

sslcontext?=?SSLContext.getInstance("TLS");

sslcontext.init(akeymanager,?atrustmanager, ?null);

sslSocketFactory?=?sslcontext.getSocketFactory();

String?url?=?"asdfdf";

URL?testURL?= ?new?URL(url);

HttpURLConnection?urlConnection?=?(HttpURLConnection)?testURL.openConnection();

if?(urlConnection?instanceof?HttpsURLConnection)?{< /p>

HttpsURLConnection?conn?=?(HttpsURLConnection)?urlConnection;

conn.setSSLSocketFactory(sslSocketFactory);

}