【问题标题】:Android ICS: Problems to get the server certificate of the HttpsUrlConnectionAndroid ICS:获取 HttpsUrlConnection 的服务器证书的问题
【发布时间】:2012-01-13 23:29:40
【问题描述】:

自 Android ICS 以来,我们在验证从 HttpsUrlConnection 获得的证书时遇到问题。在早期版本的 android 中,这运行良好。 这就是我们正在尝试做的事情:

BrowserCompatHostnameVerifier hostNameVerifier = new BrowserCompatHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hostNameVerifier);
URL url = new URL(serverUrl);
this.urlConnection = (HttpsURLConnection) url.openConnection();
this.urlConnection.connect();
hostNameVerifier.verify(urlConnection.getURL().getHost(),
(X509Certificate) urlConnection.getServerCertificates()[0]);

抛出的异常是:

java.lang.IllegalStateException 在 libcore.net.http.HttpEngine.getCacheResponse(HttpEngine.java:412) 在 libcore.net.http.HttpsURLConnectionImpl$HttpUrlConnectionDelegate.getCacheResponse(HttpsURLConnectionImpl.java:390) 在 libcore.net.http.HttpsURLConnectionImpl.getServerCertificates(HttpsURLConnectionImpl.java:87)

有人知道哪里出了问题以及为什么它只在 ICS 之后持续存在吗?

谢谢!

----- 更新------ 现在我像这样制作了自己的 HostnameVerifier。我避免使用这样的 getServerCertificates() 方法并且它正在工作:

    public class MyHostNameVerifier implements HostnameVerifier {

    private String expectedHost;

    public MyHostNameVerifier(String expectedHost) {
        this.expectedHost = expectedHost;
    }

    @Override
    public boolean verify(String hostname, SSLSession session) {
        return expectedHost.equals(hostname);
    }
}

【问题讨论】:

    标签: android ssl android-4.0-ice-cream-sandwich


    【解决方案1】:

    尝试查看以下与您的问题相关的链接。

    http://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html

    Android: HTTPS (SSL) connection using HttpsURLConnection

    httpclient ssl certificate on android

    KeyStore keyStore = ...;
     TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
     tmf.init(keyStore);
     SSLContext context = SSLContext.getInstance("TLS");
     context.init(null, tmf.getTrustManagers(), null);
     URL url = new URL("https://www.example.com/");
     HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
     urlConnection.setSSLSocketFactory(context.getSocketFactory());
     InputStream in = urlConnection.getInputStream();
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2020-03-24
    • 1970-01-01
    相关资源
    最近更新 更多