【问题标题】:Android Verify SSL sertificateAndroid 验证 SSL 证书
【发布时间】:2011-12-13 08:14:57
【问题描述】:

我的应用程序中的 SSL 验证有点问题。正如我通过互联网在 FourSquare 的文档中找到的那样,他们建议像这样进行验证:SSL Calidation,但问题是我使用的是HttpsURLConnection,我想使用该类而不是DefaultHttpClient。因此,当我用HttpsURLConnection 替换getTolerantClient 中的客户端并尝试这样做时,它向我显示了一些错误:

    public HttpsURLConnection getTolerantClient() {
    HttpsURLConnection client = new HttpsURLConnection();
    SSLSocketFactory sslSocketFactory = (SSLSocketFactory) client.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
    final X509HostnameVerifier delegate = sslSocketFactory.getHostnameVerifier();
    if(!(delegate instanceof MyVerifier)) {
        sslSocketFactory.setHostnameVerifier(new MyVerifier(delegate));
    }
    return client;
}

喜欢:

The method getConnectionManager() is undefined for the type HttpsURLConnection

&

The method getHostnameVerifier() is undefined for the type SSLSocketFactory

那么知道如何在我的情况下使用它吗? 提前致谢!

【问题讨论】:

    标签: android ssl https


    【解决方案1】:

    此代码示例应该可以帮助您适应getTolerantClient 方法:

    public URLConnection getTolerantClient(URL url) throws IOException {
        URLConnection conn = url.openConnection();
        if (!(conn instanceof HttpsURLConnection)) {
            /* not an https:// URL, nothing to do */
            return conn;
        }
        HttpsURLConnection httpsconn = (HttpsURLConnection)conn;
        final HostnameVerifier delegate = httpsconn.getHostnameVerifier();
        if(!(delegate instanceof MyVerifier)) {
            httpsconn.setHostnameVerifier(new MyVerifier(delegate));
        }
        return conn;
    }
    

    当然,MyVerifier 应该实现javax.net.ssl.HostnameVerifier 接口。

    【讨论】:

      猜你喜欢
      • 2016-06-30
      • 2017-06-13
      • 2019-03-29
      • 2011-08-19
      • 2014-09-24
      • 2012-03-24
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      相关资源
      最近更新 更多