相关疑难解决方法(0)

java.io.IOException:未验证主机名

我正在尝试从Andorid版本4.1.1中的Android应用程序连接到URL,我收到了问题标题中指出的错误,但是当我尝试从Andorid版本4.0.4或3.1连接相同的URL时一切正常.

代码片段:

    try {
        .
        .
        .
        URL url = new URL(urlStr);
        Log.i(TAG,"[ URL ] " + urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        int size = conn.getContentLength();
        int responsecode = conn.getResponseCode();
        Log.d(TAG, "Responsecode: " + responsecode);
        .
        .
        .
        } catch (Exception e) {
        e.printStackTrace();
        }


private static void trustAllHosts() {

        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new java.security.cert.X509Certificate[] {};
            }

            public void checkClientTrusted(X509Certificate[] chain,
                            String authType) throws CertificateException {
            }

            public …
Run Code Online (Sandbox Code Playgroud)

android http

40
推荐指数
5
解决办法
5万
查看次数

标签 统计

android ×1

http ×1