java客户端怎么访问带有pfx格式证书的https网站(服务器)呢,
java客户端怎么访问带有pfx格式证书的https网站(服务器)呢,看网上的资料好像都不太好使,急等。。。。。。...
java客户端怎么访问带有pfx格式证书的https网站(服务器)呢,看网上的资料好像都不太好使,急等。。。。。。
展开
2个回答
展开全部
使用HttpURLConnection 访问https地址。
以下是导入JKS证书的方式,可以参考。
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) {
HttpsURLConnection conn = (HttpsURLConnection) urlConnection;
conn.setSSLSocketFactory(sslSocketFactory);
}
更多追问追答
追问
返回 No Trusted certificate found,我的证书已经过转换成jks了,密码也对的,证书的路径有要求么?
返回 No Trusted certificate found,我的证书已经过转换成jks了,密码也对的,证书的路径有要求么?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询