排球SSL - 主机名没有被验证 [英] Volley SSL - Hostname was not verified

查看:559
本文介绍了排球SSL - 主机名没有被验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,我需要访问一个HTTPS地址。我使用的是排球,请求我的数据,但现在我得到这个错误 com.android.volley.NoConnectionError:java.io.IOException异常:主机名<地址>未验证

要获得SSL工厂我有这样的:

 私有静态的SSLSocketFactory在getSocketFactory(){
    SSLSocketFactory的的SocketFactory = NULL;


    尝试 {
        最终CertificateFactory certificateFactory = CertificateFactory.getInstance(X.509);
        最后的BufferedInputStream双=新的BufferedInputStream(sContext.getResources()openRawResource(R.raw.cert)。);

        证书;
        尝试 {
            证书= certificateFactory.generateCertificate(之二);
        } 最后 {
            bis.close();
        }

        最后弦乐keyStoreType = KeyStore.getDefaultType();
        最后的密钥库的keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(NULL,NULL);
        keyStore.setCertificateEntry(CA,证书);

        最后弦乐tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        的TrustManagerFactory TMF = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(的keyStore);
        最后的SSL连接的SSL连接= SSLContext.getInstance(TLS);
        sslContext.init(空,tmf.getTrustManagers(),NULL);
        的SocketFactory = sslContext.getSocketFactory();
    }赶上(例外五){
        Log.w(TAG,Log.getStackTraceString(e)条);
    }
    返回的SocketFactory;
}
 

队列初始化:

 最后HttpStack堆栈=新HurlStack(空,在getSocketFactory());
最后缓存缓存=新的非缓存();
最终网网络=新BasicNetwork(栈);
sRequestQueue =新的请求队列(缓存,网络);
sRequestQueue.start();
 

下面的堆栈跟踪:

  com.android.volley.NoConnectionError:java.io.IOException异常:主机名URL不验证
九月四号11:15:33.198的W / System.err的:在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
九月四号11:15:33.198的W / System.err的:在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
11月九日至4日:15:33.198 W / System.err的:java.io.IOException异常:产生的原因主机名hml.services.vivara.com.br'未核实
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.Connection.connect(Connection.java:151)
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
九月四号11:15:33.198的W / System.err的:在com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
九月四号11:15:33.198的W / System.err的:在com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:240)
九月四号11:15:33.198的W / System.err的:在com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
九月四号11:15:33.198的W / System.err的:在com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
九月四号11:15:33.198的W / System.err的:在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
 

我搜索的错误,但我没有发现任何对我的情况。谁能帮我?

解决方案

让我们假设你的服务器应用程序托管服务器的机器,有一个服务器证书,其中里面的颁发给本地主机为例。随后,里面的验证方法,你可以验证本地主机

 的HostnameVerifier的HostnameVerifier =新的HostnameVerifier(){
    @覆盖
    公共布尔验证(字符串主机名,的SSLSession会话){
        的HostnameVerifier HV =
            HttpsURLConnection.getDefaultHostnameVerifier();
        返回hv.verify(localhost,则会议);
    }
};
 

您可以阅读以下链接:

  1. 的HostnameVerifier

      

    有一个握手期间使用如果URL的主机名不匹配等方的识别主机名。

  2. 常见的有主机名验证问题

      

    原因之一可能发生这种情况是由于服务器配置错误。该   服务器配置了不具有的受试者的证书   或者符合你的服务器使用者备用名称字段   试图达成...

然后,在你的排球应用,你可以创建一个 HurlStack ,覆盖的createConnection setHostnameVerifier httpsURLConnection

希望这有助于!

I'm developing an Android App and I need to access an HTTPS address. I'm using Volley to request my data, but now i'm getting this error com.android.volley.NoConnectionError: java.io.IOException: Hostname '<address>' was not verified

To get the SSL factory i make this:

private static SSLSocketFactory getSocketFactory() {
    SSLSocketFactory socketFactory = null;


    try {
        final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        final BufferedInputStream bis = new BufferedInputStream(sContext.getResources().openRawResource(R.raw.cert)) ;

        Certificate certificate;
        try {
            certificate = certificateFactory.generateCertificate(bis);
        } finally {
            bis.close();
        }

        final String keyStoreType = KeyStore.getDefaultType();
        final KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", certificate);

        final String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);
        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
        socketFactory = sslContext.getSocketFactory();
    } catch (Exception e) {
        Log.w(TAG, Log.getStackTraceString(e));
    }
    return socketFactory;
}

The queue initialization:

final HttpStack stack = new HurlStack(null, getSocketFactory());
final Cache cache = new NoCache();
final Network network = new BasicNetwork(stack);
sRequestQueue = new RequestQueue(cache, network);
sRequestQueue.start();

Here's the stacktrace:

    com.android.volley.NoConnectionError: java.io.IOException: Hostname 'url' was not verified
09-04 11:15:33.198   W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
09-04 11:15:33.198   W/System.err﹕ at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
09-04 11:15:33.198   W/System.err﹕ Caused by: java.io.IOException: Hostname 'hml.services.vivara.com.br' was not verified
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:151)
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
09-04 11:15:33.198   W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
09-04 11:15:33.198   W/System.err﹕ at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:240)
09-04 11:15:33.198   W/System.err﹕ at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
09-04 11:15:33.198   W/System.err﹕ at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
09-04 11:15:33.198   W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)

I searched for the error, but I didn't find anything for my case. Can anyone help me?

解决方案

Let's assume your server app is hosting inside a server machine which has a server certificate in which "Issued to" is "localhost", for example. Then, inside verify method you can verify "localhost".

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

You can read more at the following links:

  1. HostnameVerifier

    It is to be used during a handshake if the URL's hostname does not match the peer's identification hostname.

  2. Common Problems with Hostname Verification

    One reason this can happen is due to a server configuration error. The server is configured with a certificate that does not have a subject or subject alternative name fields that match the server you are trying to reach...

Then, in your Volley app, you can create a HurlStack, overriding createConnection in which setHostnameVerifier for the httpsURLConnection.

Hope this helps!

这篇关于排球SSL - 主机名没有被验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆