2012-04-27 98 views
2

像這麼多人在這裏,我試圖與擁有自簽名證書的網站溝通。有許多類和代碼片段顯示瞭如何做到這一點,例如HTTPS GET (SSL) with Android and self-signed server certificate,但我找不到任何顯示如何實際使用類。我已經嘗試了很多,我確信我只是錯過了一些簡單的東西。Android:忽略自簽名證書錯誤;實際執行?

具體來說,我試圖使用「莫斯」提供的類;得票最多的答案。我仍然總是得到「不可信任的證書」消息。該主題中的另一個人也要求提供實施提示,但尚未得到答覆。謝謝你的幫助。

如果我可以將這個問題附加到該主題上,我會更加快樂,但我想像我這樣的新手只能發佈新的問題(儘管我一直是SO的粉絲超過一年)。

回答

2

我們在一些項目中使用類似的SocketFactoryX509TrustManager實現。爲了將這些類綁定到您的實現上,您基本上必須要做的就是將它們連接到用於客戶端 - 服務器通信的HttpClient(假設這就是您正在使用的)。

我們通常有一種方法可以用上面提到的工廠和信任管理器創建一個HttpClient。它看起來有點像這樣,並且基於內聯評論中顯示的鏈接鬆散。

protected HttpClient constructClient() { 
    HttpParams params = new BasicHttpParams(); 
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

    // use the debug proxy to view internet traffic on the host computer 
    if (ABCApplication.isDebuggingProxy()) params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(ABCConstants.DEBUG_PROXY_HOST, ABCConstants.DEBUG_PROXY_PORT, "http")); 

    // ignore ssl certification (due to signed authority not appearing on android list of permitted authorities) 
    // see: http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/ 
    SchemeRegistry registry = new SchemeRegistry(); 
    registry.register(new Scheme("http", new PlainSocketFactory(), 80)); 
    registry.register(new Scheme("https", new FakeSocketFactory(), 443)); 
    ClientConnectionManager cm = new SingleClientConnManager(params, registry); 

    return new DefaultHttpClient(cm, params); 
} 

希望能幫助您的實施。

+0

這不只是幫助,它只是工作!它看起來和我嘗試過的所有其他人一樣,但他們增加了更多的參數。他們似乎也喜歡使用ThreadSafeClientConnManager而不是Single。當我有時間時,我會教育自己的差異。感謝名單! (如果我有代表,我會+1你的答案)。 – MBro 2012-04-28 18:22:30

2

我們也可以使用

HttpURLConnection http = null; 
      URL url; 
      try { 
       url = new URL("https:your domian"); 

       if (url.getProtocol().toLowerCase().equals("https")) { 
        trustAllHosts(); 
        HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); 
        https.setHostnameVerifier(DO_NOT_VERIFY); 
        http = https; 
        System.out.println("TEST:::"+convertStreamToString(http.getInputStream())); 
       } else { 
        http = (HttpURLConnection) url.openConnection(); 
        System.out.println("TEST:::"+convertStreamToString(http.getInputStream())); 
       } 
      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


/******* 
* Trust every server - don't check for any certificate 
*/ 
private static void trustAllHosts() { 
    // Create a trust manager that does not validate certificate chains 
    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 void checkServerTrusted(X509Certificate[] chain, 
       String authType) throws CertificateException { 
     } 
    } }; 

    // Install the all-trusting trust manager 
    try { 
     SSLContext sc = SSLContext.getInstance("TLS"); 
     sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
     HttpsURLConnection 
       .setDefaultSSLSocketFactory(sc.getSocketFactory()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 



final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { 
    public boolean verify(String hostname, SSLSession session) { 
     return true; 
    } 
};