【问题标题】:Android Hostname was not be verified. How to allow all hosts?未验证 Android 主机名。如何允许所有主机?
【发布时间】:2014-11-09 17:28:07
【问题描述】:

我正在尝试从以 HTTPS 开头的 URL 获取图像。我不断收到主机名未验证异常。

我看了这个问题java.io.IOException: Hostname was not verified,但不明白如何使它工作。

有什么方法可以允许所有主机名?

这是给我带来麻烦的代码:

public Drawable drawableFromUrl(String url) {
    Bitmap x;
    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) new URL(url).openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
        x = BitmapFactory.decodeStream(input);
        return new BitmapDrawable(x);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        return null;
    }
    return null;
}

提前感谢您的帮助

【问题讨论】:

    标签: java android httpurlconnection


    【解决方案1】:

    当 TLS 证书是自签名的或证书上的域与服务器的主机名不匹配时,会发生此错误。

    This answer提供完整的解决方案:

    /**
     * Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to
     * aid testing on a local box, not for use on production.
     */
    private static void disableSSLCertificateChecking() {
        TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
    
                @Override
                public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
                    // not implemented
                }
    
                @Override
                public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
                    // not implemented
                }
    
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
    
            }
        };
    
        try {
    
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
    
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
    
            });
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    

    在进行第一次 HTTPS 连接之前,只需运行一次即可。但是,如果您控制服务器,则强烈建议您尝试获取有效证书。

    【讨论】:

    • 它不起作用。我只是在用户登录时调用了它。但是当我尝试获取图像时仍然遇到同样的错误
    • 现在我每次建立 HTTPPS 连接时都会调用它,并且我的错误更改为 FileNotFoundException,所以我想这解决了问题。你为什么说我应该只运行一次?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 2020-11-15
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多