两个包之间的 setSSLSocketFactory 面临类转换错误
我已经创建了自己的 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.SSLSocketFactory 和 之间的转换类错误>javax.net.ssl.SSLSocketFactory。
怎么解决这个问题呢?
I have created my own SSLSocketFactory as explained in this question
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);
}
}
Actually i need to set this SSLSocketFactory
on the HttpsURLConnection
before connecting. But when i try to set it on HttpsURLConnection
by calling
(HttpsURLConnection )connection.setSSLSocketFactory(trusted);
At this point am facing cast class error between 2 packages org.apache.http.conn.ssl.SSLSocketFactory
and javax.net.ssl.SSLSocketFactory
.
How to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码没有出现任何异常。
但问题是,当我尝试使用以下方法在
HttpsURLConnection
上设置SSLSocketFactory
时:它显示“方法
setSSLSocketFactory(SSLSocketFactory)HttpsURLConnection
类型中的 code> 不适用于参数(SSLSocketFactory)
”。因为两个包中都存在方法
setSSLSocketFactory
。Am not getting any exception with the above piece of code.
But the problem is that, when am trying to set the
SSLSocketFactory
on theHttpsURLConnection
using:It is showing "The method
setSSLSocketFactory(SSLSocketFactory)
in the typeHttpsURLConnection
is not applicable for the arguments(SSLSocketFactory)
".Because the method
setSSLSocketFactory
is there in both the packages.