gpt4 book ai didi

java - 在两个包之间使用 setSSLSocketFactory 面临类转换错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:24 27 4
gpt4 key购买 nike

我已经按照 this question 中的说明创建了自己的 SSLSocketFactory

private SSLSocketFactory newSslSocketFactory() {
try {
// Get an instance of the Bouncy Castle KeyStore format
KeyStore trusted = KeyStore.getInstance("BKS");
// Get theraw resource, which contains the keystore with
// your trusted certificates (root and any intermediate certs)
Context context = this.activity;
Resources _resources = context.getResources();
InputStream in = _resources.openRawResource(R.raw.mystore);
try {
// Initialize the keystore with the provided trusted certificates
// Also provide the password of the keystore
trusted.load(in, "mypassword".toCharArray());
} finally {
in.close();
}
// Pass the keystore to the SSLSocketFactory. The factory is responsible
// for the verification of the server certificate.
SSLSocketFactory sf = new SSLSocketFactory(trusted);
// Hostname verification from certificate

sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
return sf;
} catch (Exception e) {
throw new AssertionError(e);
}
}

实际上,我需要在连接之前在 HttpsURLConnection 上设置此 SSLSocketFactory。但是,当我尝试通过调用

将其设置为 HttpsURLConnection
(HttpsURLConnection )connection.setSSLSocketFactory(trusted); 

此时我正面临 2 个包 org.apache.http.conn.ssl.SSLSocketFactoryjavax.net.ssl.SSLSocketFactory 之间的转换类错误。

如何解决?

最佳答案

上面的代码没有任何异常。

但问题是,当我尝试在 HttpsURLConnection 上设置 SSLSocketFactory 时,使用:

(HttpsURLConnection )connection.setSSLSocketFactory(trusted) 

它显示“HttpsURLConnection 类型的方法setSSLSocketFactory(SSLSocketFactory) 不适用于参数(SSLSocketFactory) ”。

因为 setSSLSocketFactory 方法在两个包中都存在。

关于java - 在两个包之间使用 setSSLSocketFactory 面临类转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7160174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com