2012-08-27 92 views
1

我的代碼適用於http,但不適用於https。 我正在嘗試使用HttpClient在Android手機上製作Https connections。麻煩的是我不斷收到net.ssl.SSLPeerUnverifiedException: No peer certificate。和Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.如何創建https連接?

這裏是Custom HttpClient的相關代碼。

public static HttpClient getNewHttpClient() { 

    HttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
    HttpProtocolParams.setUseExpectContinue(params, true); 

    SchemeRegistry schReg = new SchemeRegistry(); 
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); 

    DefaultHttpClient http = new DefaultHttpClient(conMgr, params); 
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("name", "pass"); 
    AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); 
    http.getCredentialsProvider().setCredentials(authScope, credentials); 

    return http; 
} 

這是從服務器

public static void Get() { 

    HttpClient http = getNewHttpClient();  

    HttpResponse response; 
    try { 
     HttpGet httpost = new HttpGet(url); 
     response = http.execute(httpost); 
     BufferedReader in = null; 
     Log.i(TAG, "resp-" + response); 
     in = new BufferedReader(new InputStreamReader(response.getEntity() 
       .getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 
     String page = sb.toString(); 
     Log.i(TAG, "res=" + sb.toString()); 
     tv.setText(page); 
    } catch (ClientProtocolException e) { 
     Log.i(TAG, "ClientProtocolException=" + e); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     Log.i(TAG, "ClientProtocolException=" + e); 
     e.printStackTrace(); 
    } 
} 

任何想法獲取信息的代碼?

+0

我收到此錯誤:javax.net.ssl.SSLPeerUnverifiedException:沒有對方的證書 –

+0

HTTP:/ /developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html 2個月前,我從上述鏈接的參考中解決了我的問題。 –

回答

2

最後我得到了解決我的問題,這個解決方案我在三天內搜查了很多。使用用戶名和密碼驗證服務器時出現問題。我改變了這樣的代碼。我傳遞的憑據是在比問題可用的代碼等我的代碼只變更的服務器..

public static HttpClient _getNewHttpClient() { 
    try { 
     KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
     trustStore.load(null, null); 

     SSLSocketFactory sf = new MySSLSocketFactory(trustStore); 
     sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

     HttpParams params = new BasicHttpParams(); 
     HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
     HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 

     SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
     registry.register(new Scheme("https", sf, 443)); 

     ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 

     DefaultHttpClient http = new DefaultHttpClient(ccm, params); 
     UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("jk", "jk"); 
     AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); 
     http.getCredentialsProvider().setCredentials(authScope, credentials); 

     return http; 
    } catch (Exception e) { 
     return new DefaultHttpClient(); 
    } 
} 
+0

添加一個帶有空信任庫的TrustManager無法解決此問題。更可能的解決方案在於你的'MySSLSocketFactory'的未公開魔術。 – EJP

+0

我嘗試了所有可用的解決方案,最終我從這個http://stackoverflow.com/a/4837230/964741 –

+0

得到解決方案感謝它爲我工作。 –

-1

嘗試尋找如何HTTPS過的HttpClient在這個職位的答案被實施:Trusting all certificates using HttpClient over HTTPS

他們都談到了詳細的密鑰存儲和的TrustManager

+0

我讀過這些答案。仍然我對keystore感到困惑......通過使用信任管理器,我得到未經授權的錯誤... –