未在org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname </url>验证主机名<url>

时间:2014-07-07 06:28:02

标签: java android apache https

我使用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

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

当我更改测试URL以发布URL时,应用程序正在提供以下异常

  

07-07 11:06:17.515:W / System.err(23000):java.io.IOException:Hostname   &#39; ...........&#39;未经核实07-07 11:06:17.515:   W / System.err(23000):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:199)   07-07 11:06:17.515:W / System.err(23000):at   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):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)   07-07 11:06:17.515:W / System.err(23000):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)   07-07 11:06:17.515:W / System.err(23000):at   org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268)   07-07 11:06:17.515:W / System.err(23000):at   com.globalsolutions.mygov.api.rest.SimpleRestClient.sendRequest(SimpleRestClient.java:69)   07-07 11:06:17.515:W / System.err(23000):at   com.globalsolutions.mygov.api.rest.SimpleRestClient.post(SimpleRestClient.java:41)   07-07 11:06:17.515:W / System.err(23000):at   com.globalsolutions.mygov.api.Api.auth(Api.java:117)07-07   11:06:17.515:W / System.err(23000):at   com.globalsolutions.mygov.ui.SplashActivity $ 6.run(SplashActivity.java:248)   07-07 11:06:17.531:W / System.err(23000):at   java.lang.Thread.run(Thread.java:1019)07-07 11:06:17.531:   W / System.err(23000):java.io.IOException:Hostname&#39; my.gov.uz&#39;不是   已验证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):at   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):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)   07-07 11:06:17.539:W / System.err(23000):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)   07-07 11:06:17.539:W / System.err(23000):at   org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268)   07-07 11:06:17.539:W / System.err(23000):at   com.globalsolutions.mygov.api.rest.SimpleRestClient.sendRequest(SimpleRestClient.java:69)   07-07 11:06:17.554:W / System.err(23000):at   com.globalsolutions.mygov.api.rest.SimpleRestClient.post(SimpleRestClient.java:41)   07-07 11:06:17.554:W / System.err(23000):at   com.globalsolutions.mygov.api.Api.auth(Api.java:117)07-07   11:06:17.554:W / System.err(23000):at   com.globalsolutions.mygov.ui.SplashActivity $ 6.run(SplashActivity.java:248)   07-07 11:06:17.554:W / System.err(23000):at   java.lang.Thread.run(Thread.java:1019)

我认为这一行导致错误:

conn.getOutputStream().write(payload);
我用Google搜索了这个例子,找到了不同的建议。老实说,我不明白如何将这些代码实现到我的应用程序中。

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

以防万一,我正在使用Samsung Galaxy Ace和Android 2.3.6

更新:使用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:Hostname   &#39; my.gov.uz&#39;未经核实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):at   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):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)   07-07 12:39:30.890:W / System.err(25360):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)   07-07 12:39:30.890:W / System.err(25360):at   org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268)   07-07 12:39:30.890:W / System.err(25360):at   com.globalsolutions.mygov.api.rest.SimpleRestClient.sendRequest(SimpleRestClient.java:85)   07-07 12:39:30.890:W / System.err(25360):at   com.globalsolutions.mygov.api.rest.SimpleRestClient.post(SimpleRestClient.java:46)   07-07 12:39:30.890:W / System.err(25360):at   com.globalsolutions.mygov.api.Api.auth(Api.java:117)07-07   12:39:30.890:W / System.err(25360):at   com.globalsolutions.mygov.ui.SplashActivity $ 6.run(SplashActivity.java:248)   07-07 12:39:30.890:W / System.err(25360):at   java.lang.Thread.run(Thread.java:1019)07-07 12:39:30.898:   W / System.err(25360):java.io.IOException:Hostname&#39; my.gov.uz&#39;不是   已验证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):at   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):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)   07-07 12:39:30.898:W / System.err(25360):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)   07-07 12:39:30.898:W / System.err(25360):at   org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268)   07-07 12:39:30.898:W / System.err(25360):at   com.globalsolutions.mygov.api.rest.SimpleRestClient.sendRequest(SimpleRestClient.java:85)   07-07 12:39:30.898:W / System.err(25360):at   com.globalsolutions.mygov.api.rest.SimpleRestClient.post(SimpleRestClient.java:46)   07-07 12:39:30.921:W / System.err(25360):at   com.globalsolutions.mygov.api.Api.auth(Api.java:117)07-07   12:39:30.921:W / System.err(25360):at   com.globalsolutions.mygov.ui.SplashActivity $ 6.run(SplashActivity.java:248)   07-07 12:39:30.921:W / System.err(25360):at   java.lang.Thread.run(Thread.java:1019)

1 个答案:

答案 0 :(得分:0)

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