两个包之间的 setSSLSocketFactory 面临类转换错误

发布于 2024-11-30 20:32:56 字数 1700 浏览 0 评论 0原文

我已经创建了自己的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2024-12-07 20:32:56

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

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

(HttpsURLConnection )connection.setSSLSocketFactory(trusted) 

它显示“方法 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 the HttpsURLConnection using:

(HttpsURLConnection )connection.setSSLSocketFactory(trusted) 

It is showing "The method setSSLSocketFactory(SSLSocketFactory) in the type HttpsURLConnection is not applicable for the arguments(SSLSocketFactory)".

Because the method setSSLSocketFactory is there in both the packages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文