2015-09-04 61 views
3

我正在开发Android应用程序,我需要访问HTTPS地址。我使用抽射要求我的数据,但现在我得到这个错误com.android.volley.NoConnectionError: java.io.IOException: Hostname '<address>' was not verifiedVolley SSL - 主机名未验证

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

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; 
} 

队列初始化:

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(); 

这里的堆栈跟踪:

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) 

我搜索了错误,但我没有找到任何东西为我的情况。谁能帮我?

回答

4

让我们假设您的服务器应用程序托管在服务器机器中,该服务器机器具有服务器证书,其中"Issued to""localhost",例如。然后,在验证方法内部,您可以验证"localhost"

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

您可以通过以下链接阅读更多:

  1. HostnameVerifier

    这是一个握手过程中使用,如果URL的主机名不匹配对等的标识主机。

  2. Common Problems with Hostname Verification

    原因之一可能发生这种情况是由于服务器配置错误。该 服务器配置不具备与你 尝试访问的服务器,一个主题 或主题备用名称字段的证书......

然后,在你排球应用,您可以创建一个HurlStack,覆盖createConnection其中setHostnameVerifierhttpsURLConnection

希望这会有所帮助!

+0

的方法发现这个错误的原因'HurlStack'中的'createConnection'返回'HttpURLConnection',而不是'HttpsURLConnection'。所以,没有'setHostnameVerifier'。请提供更详细的回答如何为这种情况创建一个自定义的'HurlStack'对象。谢谢! – toom

+1

@toom你可以在我的另一个答案http:// stackoverflow找到一个例子。com/questions/32673568/do-android-volley-support-ssl/32674422#32674422 – BNK

+1

你的其他答案非常好。谢谢! – toom

-2

当您的网络在http请求期间断开连接时发生此错误。 所以请确保您的互联网是一致的,然后检查。

+0

我认为你可以在https://developer.android.com/training/articles/security-ssl.html#CommonHostnameProbs – BNK

-2

这是一个可能的解决方案

HostnameVerifier allHostsValid = new HostnameVerifier() { 
    public boolean verify(String hostname, SSLSession session) { 
     return true; 
    } 
}; 
+0

那应该是什么? –