2010-06-16 35 views
5

Tôi nhận được ngoại lệ này khi tôi đang cố gắng để đạt HTTPS URL (Net Webservice) từ ứng dụng Android của tôi:"javax.net.ssl.SSLException: Không đáng tin cậy chứng chỉ máy chủ" ngoại lệ trong Android

javax .net.ssl.SSLException: Không đáng tin cậy chứng chỉ máy chủ

đây là mã của tôi:

HttpParams myParams = new BasicHttpParams(); 
HttpProtocolParams.setVersion(myParams, HttpVersion.HTTP_1_1); 
HttpProtocolParams.setContentCharset(myParams, "utf-8"); 
myParams.setBooleanParameter("http.protocol.expect-continue", false); 


HttpConnectionParams.setConnectionTimeout(myParams, 100000); 
HttpConnectionParams.setSoTimeout(myParams, 100000); 

// 
KeyStore trusted; 
try { 

     trusted = KeyStore.getInstance("pkcs12"); 
     trusted.load(null, "".toCharArray()); 
     SSLSocketFactory sslf = new SSLSocketFactory(trusted); 
    sslf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

    SchemeRegistry schemeRegistry = new SchemeRegistry(); 
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 

    schemeRegistry.register(new Scheme("https", sslf, 443));  
    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(myParams, schemeRegistry); 


    httpClient = new DefaultHttpClient(manager, myParams); 
    } catch (KeyStoreException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (NoSuchAlgorithmException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (CertificateException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (KeyManagementException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (UnrecoverableKeyException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    }   
    // 

    localContext = new BasicHttpContext(); 

    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); 

    httpPost = new HttpPost("https://...."); 
    response = null; 

    StringEntity tmp = null;   

    httpPost.setHeader("SOAPAction", "SoapActionURL"); 

    if (contentType != null) { 
     httpPost.setHeader("Content-Type", contentType); 
    } else { 
     httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
    } 

    try { 
     tmp = new StringEntity(data,"UTF-8"); 
    } catch (UnsupportedEncodingException e) { 
     Log.e("Your App Name Here", "HttpUtils : UnsupportedEncodingException : "+e); 
    } 

    httpPost.setEntity(tmp); 

    try { 

     response = httpClient.execute(httpPost,localContext); 

     if (response != null) { 
      ret = EntityUtils.toString(response.getEntity()); 
     } 
    } catch (Exception e) { 
     Log.e("Your App Name Here", "HttpUtils: " + e); 
    } 
+1

Xem câu hỏi http://stackoverflow.com/questions/995514/https-connection-android#1000205 –

+0

Khi bạn thực hiện một cuộc gọi https bạn thực sự cần sự hiện diện của một chứng chỉ. Bạn có chứng chỉ có trên webservice của mình không? – Droidekas

Trả lời

1
KeyStore trusted; 
try { 

     trusted = KeyStore.getInstance("pkcs12"); 
     trusted.load(null, "".toCharArray()); 
     SSLSocketFactory sslf = new SSLSocketFactory(trusted); 

Bạn không có một truststore lưu trữ chứng chỉ của máy chủ mà bạn đang kết nối đến, và máy chủ mà bạn đang kết nối đến không đáng tin cậy. Để khắc phục điều này, bạn cần tạo một tệp kho khóa bằng cách sử dụng Keytool (Tôi đã tạo một kho khóa BKS và tải nó với nhà cung cấp SpongyCastle) làm một truststore.

keytool -importcert -file servercert.crt -alias mykey -keystore truststore.jks -storetype BKS -providerpath bcprov-jdk15on-151.jar -provider org.bouncycastle.jce.provider.BouncyCastleProvider 

Ngoài ra, tôi sử dụng một phiên bản mới hơn của Apache HttpClient, cụ thể http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient-android/4.3.5

public KeyStore initializeTrustStore(Context context) 
{ 
    KeyStore keyStore = null; 
    try 
    { 
     keyStore = KeyStore.getInstance("BKS", BouncyCastleProvider.PROVIDER_NAME); 
     InputStream inputStream = context.getResources().openRawResource(R.raw.truststore); 
     try 
     { 
      keyStore.load(inputStream, "password".toCharArray()); 
     } 
     finally 
     { 
      inputStream.close(); 
     } 
    } 


    KeyStore trustStore = loadTrustStore(context); 
    SSLContext sslcontext = null; 
    CloseableHttpClient httpclient = null; 

     SSLContextBuilder sslContextBuilder = SSLContexts.custom() 
      .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()); 
     sslcontext = sslContextBuilder.build(); 

     // Allow TLSv1 protocol only 
     SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
      sslcontext, new String[] {"TLSv1"}, null, 
      SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER 
     ); 
     httpclient = HttpClients 
      .custom() 
      .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) 
      .setSSLSocketFactory(sslsf).build(); 
     CloseableHttpResponse response = httpclient.execute(httpUriRequest); 
     try 
     { 
      HttpEntity entity = response.getEntity(); 
      //do things with response here 
      if(entity != null) 
      { 
       entity.consumeContent(); 
      } 
     } 
     finally 
     { 
      response.close(); 
     } 
Các vấn đề liên quan