【问题标题】:Hostname <URL> was not verified at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname主机名 <URL> 未在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname 验证
【发布时间】:2014-08-27 13:25:04
【问题描述】:

我使用 HTTP 从网站的 API 获取 JSON。这是开发测试 URL。

public static final String BASE_URL = "http://...."

我正在使用 POST 请求。

private Response sendRequest(Request request, boolean debug)
            throws Exception {

        HttpURLConnection conn = null;

        Response response = null;
        long time = 0;

        try {
            conn = (HttpURLConnection) request.getUrl().openConnection();

            if (request.headers != null) {
                for (String header : request.headers.keySet()) {
                    conn.addRequestProperty(header, request.headers.get(header));
                }
            }

            time = System.currentTimeMillis();

            conn.setDoOutput(false);
            if (request instanceof POST) {
                byte[] payload = ((POST) request).body;

                conn.setDoOutput(true);
                conn.setFixedLengthStreamingMode(payload.length);
                conn.getOutputStream().write(payload);//line69
            }

            int status = conn.getResponseCode();

            if (status != HttpURLConnection.HTTP_OK)
                response = new Response(status, conn.getResponseMessage()
                        .getBytes());
            else {
                if (conn.getContentType().contains("application/json")) {
                    response = new Response(status, readInputStream(
                            conn.getInputStream()).getBytes());
                } else {
                    response = new Response(status,
                            readInputStreamWithoutUTF8(conn.getInputStream()));
                }

            }
            response.contentType = conn.getContentType();
            response.contentLength = conn.getContentLength();
            response.time = System.currentTimeMillis() - time;
            if (debug)
                dumpRequest(request, response);

        } catch (IOException e) {
            e.printStackTrace(System.err);
            throw e;

        } finally {
            if (conn != null)
                conn.disconnect();
        }

        return response;
    }

测试 URL 一切正常。但是文档说要使用发布 URL,即

public static final String BASE_URL = "https://..."

当我将测试 URL 更改为发布 URL 时,应用程序出现以下异常

07-07 11:06:17.515: W/System.err(23000): java.io.IOException: 主机名 '............' 未验证 07-07 11:06:17.515: W / System.err(23000):在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:199) 07-07 11:06:17.515: W/System.err(23000): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:391) 07-07 11:06:17.515: W/System.err(23000): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 07-07 11:06:17.515: W/System.err(23000): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614) 07-07 11:06:17.515: W/System.err(23000): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268) 07-07 11:06:17.515: W/System.err(23000): 在 com.globalsolutions.mygov.api.rest.SimpleRestClient.sendRequest(SimpleRestClient.java:69) 07-07 11:06:17.515: W/System.err(23000): 在 com.globalsolutions.mygov.api.rest.SimpleRestClient.post(SimpleRestClient.java:41) 07-07 11:06:17.515: W/System.err(23000): 在 com.globalsolutions.mygov.api.Api.auth(Api.java:117) 07-07 11:06:17.515:W/System.err(23000):在 com.globalsolutions.mygov.ui.SplashActivity$6.run(SplashActivity.java:248) 07-07 11:06:17.531: W/System.err(23000): 在 java.lang.Thread.run(Thread.java:1019) 07-07 11:06:17.531: W/System.err(23000): java.io.IOException: Hostname 'my.gov.uz' 不是 已验证 07-07 11:06:17.539: W/System.err(23000): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:199) 07-07 11:06:17.539: W/System.err(23000): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:391) 07-07 11:06:17.539: W/System.err(23000): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 07-07 11:06:17.539: W/System.err(23000): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614) 07-07 11:06:17.539: W/System.err(23000): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268) 07-07 11:06:17.539: W/System.err(23000): 在 com.globalsolutions.mygov.api.rest.SimpleRestClient.sendRequest(SimpleRestClient.java:69) 07-07 11:06:17.554: W/System.err(23000): 在 com.globalsolutions.mygov.api.rest.SimpleRestClient.post(SimpleRestClient.java:41) 07-07 11:06:17.554: W/System.err(23000): 在 com.globalsolutions.mygov.api.Api.auth(Api.java:117) 07-07 11:06:17.554:W/System.err(23000):在 com.globalsolutions.mygov.ui.SplashActivity$6.run(SplashActivity.java:248) 07-07 11:06:17.554: W/System.err(23000): 在 java.lang.Thread.run(Thread.java:1019)

我认为这行导致错误:

conn.getOutputStream().write(payload);

我搜索了这个例外,发现了不同的建议。老实说,我不明白如何在我的应用程序中实现这些代码。

有人可以解释如何将 HTTP 连接更改为 HTTPS 连接吗? 感谢您宝贵的时间!

以防万一,我使用的是带有 Android 2.3.6 的三星 Galaxy Ace

更新:使用 HostnameVerifier 更新代码

private Response sendRequest(Request request, boolean debug)
            throws Exception {

        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                HostnameVerifier hv =
                    HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify("my.gov.uz", session);
            }
        };

        HttpsURLConnection conn = null;

        Response response = null;
        long time = 0;

        try {

            conn = (HttpsURLConnection) request.getUrl().openConnection();
            conn.setHostnameVerifier(hostnameVerifier);

            if (request.headers != null) {
                for (String header : request.headers.keySet()) {
                    conn.addRequestProperty(header, request.headers.get(header));
                }
            }

            time = System.currentTimeMillis();

            conn.setDoOutput(false);
            if (request instanceof POST) {
                byte[] payload = ((POST) request).body;

                conn.setDoOutput(true);
                conn.setFixedLengthStreamingMode(payload.length);
                conn.getOutputStream().write(payload);
            }

            int status = conn.getResponseCode();

            if (status != HttpURLConnection.HTTP_OK)
                response = new Response(status, conn.getResponseMessage()
                        .getBytes());
            else {
                if (conn.getContentType().contains("application/json")) {
                    response = new Response(status, readInputStream(
                            conn.getInputStream()).getBytes());
                } else {
                    response = new Response(status,
                            readInputStreamWithoutUTF8(conn.getInputStream()));
                }

            }
            response.contentType = conn.getContentType();
            response.contentLength = conn.getContentLength();
            response.time = System.currentTimeMillis() - time;
            if (debug)
                dumpRequest(request, response);

        } catch (IOException e) {
            e.printStackTrace(System.err);
            throw e;

        } finally {
            if (conn != null)
                conn.disconnect();
        }

        return response;
    }

但仍然得到相同的例外:

07-07 12:39:30.882: W/System.err(25360): java.io.IOException: 主机名 'my.gov.uz' 未验证 07-07 12:39:30.890: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:199) 07-07 12:39:30.890: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:391) 07-07 12:39:30.890: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 07-07 12:39:30.890: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614) 07-07 12:39:30.890: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268) 07-07 12:39:30.890: W/System.err(25360): 在 com.globalsolutions.mygov.api.rest.SimpleRestClient.sendRequest(SimpleRestClient.java:85) 07-07 12:39:30.890: W/System.err(25360): 在 com.globalsolutions.mygov.api.rest.SimpleRestClient.post(SimpleRestClient.java:46) 07-07 12:39:30.890: W/System.err(25360): 在 com.globalsolutions.mygov.api.Api.auth(Api.java:117) 07-07 12:39:30.890:W/System.err(25360):在 com.globalsolutions.mygov.ui.SplashActivity$6.run(SplashActivity.java:248) 07-07 12:39:30.890: W/System.err(25360): 在 java.lang.Thread.run(Thread.java:1019) 07-07 12:39:30.898: W/System.err(25360): java.io.IOException: Hostname 'my.gov.uz' 不是 已验证 07-07 12:39:30.898: W/System.err(25360): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:199) 07-07 12:39:30.898: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:391) 07-07 12:39:30.898: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 07-07 12:39:30.898: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614) 07-07 12:39:30.898: W/System.err(25360): 在 org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268) 07-07 12:39:30.898: W/System.err(25360): 在 com.globalsolutions.mygov.api.rest.SimpleRestClient.sendRequest(SimpleRestClient.java:85) 07-07 12:39:30.898: W/System.err(25360): 在 com.globalsolutions.mygov.api.rest.SimpleRestClient.post(SimpleRestClient.java:46) 07-07 12:39:30.921: W/System.err(25360): 在 com.globalsolutions.mygov.api.Api.auth(Api.java:117) 07-07 12:39:30.921:W/System.err(25360):在 com.globalsolutions.mygov.ui.SplashActivity$6.run(SplashActivity.java:248) 07-07 12:39:30.921: W/System.err(25360): 在 java.lang.Thread.run(Thread.java:1019)

【问题讨论】:

    标签: java android apache https


    【解决方案1】:

    您的服务器配置的证书似乎没有与您尝试访问的服务器匹配的主题或主题备用名称字段。尝试this 以避免此问题

    【讨论】:

    • 我无权访问服务器。我只能从客户端访问。在这种情况下我该怎么办?
    • 您是否阅读过链接后面的文档?它描述了如何在客户端解决问题。它建议使用HttpsUrlConnection 或使用自定义HostnameVerifier,以防万一here is the link I was talking about
    • 我使用了 hostNameVerfier 选项: HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();返回 hv.verify("my.gov.uz", session); } }; HttpsURLConnection 连接 = 空;响应响应 = null;长时间 = 0;尝试 { conn = (HttpsURLConnection) request.getUrl().openConnection(); conn.setHostnameVerifier(hostnameVerifier);但它仍然给出同样的例外。
    • 其实很奇怪,这应该可以工作,你确定Exception是一样的吗?能否请您使用 HostnameVerifier 发布更新的代码?
    猜你喜欢
    • 2015-08-25
    • 2020-11-15
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 2019-01-20
    • 2021-09-19
    • 2018-05-24
    相关资源
    最近更新 更多