2012-08-29 51 views
7

Khi tôi muốn mở kết nối HTTPS, tôi nhận được Ngoại lệ SSL. Làm thế nào để thiết lập HttpURLConnection theo cách không nhạy cảm với ngoại lệ này?Tắt xác thực chứng chỉ SSL của kết nối HTTPS?

Mã của tôi là:

private String getData() { 
    String response = null; 
    String connection = "https://www.kamalan.com/"; 

    try { 
     URL url = new URL(connection); 
     Log.i(TAG, "Try to open: " + connection); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

     int responseCode = conn.getResponseCode(); 
     Log.i(TAG, "Response code is: " + responseCode); 
     if (responseCode == HttpURLConnection.HTTP_OK) { 
      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      if (in != null) { 
       StringBuilder strBuilder = new StringBuilder();    
       int ch = 0; 
       while ((ch = in.read()) != -1) 
        strBuilder.append((char) ch); 

       // get returned message and show it 
       response = strBuilder.toString(); 
       Log.i("JSON returned by server:", response); 
      } 

      in.close(); 

     } else { 
      Log.e(TAG, "Couldn't open connection in getResepiItems()"); 
     } 
    } catch (SSLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return response; 
} 
+0

nào logcat nói gì? –

+0

@Morrison Chang Đây là lỗi trong con mèo đăng nhập 'Gây ra bởi: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Niềm tin neo cho đường dẫn chứng nhận không tìm thấy.' –

+1

Bạn đã thấy bài đăng SO này: http: //stackoverflow.com/questions/6825226/trust-anchor-not-found-for-android-ssl-connection? –

Trả lời

0

Thực hiện theo các phương pháp dưới đây, nó làm việc cho tôi.

 URL url = new URL("Your URL"); 
     HttpsURLConnection urlConnection =(HttpsURLConnection) url.openConnection(); urlConnection.setSSLSocketFactory(SSLCertificateSocketFactory.getInsecure(0, null)); 
     urlConnection.setHostnameVerifier(getHostnameVerifier()); 
     InputStream is = urlConnection.getInputStream(); 
     OutputStream os = new FileOutputStream(downloadedFile); 
     byte[] data = new byte[1024]; 
     int count; 
     while ((count = is.read(data)) != -1) { 
      os.write(data, 0, count); 
     } 
     os.flush(); 
     os.close(); 
     is.close(); 

Phương pháp dưới đây để thiết lập các Hostname

private HostnameVerifier getHostnameVerifier() { 
     HostnameVerifier hostnameVerifier = new HostnameVerifier() { 
      @Override 
      public boolean verify(String hostname, SSLSession session) { 
       HostnameVerifier hv = 
         HttpsURLConnection.getDefaultHostnameVerifier(); 
       return hv.verify("com.example.com", session); 
      } 
     }; 
     return hostnameVerifier; 
    } 
+0

Nó hoạt động như thế nào? Và tại sao? Tất cả số tiền vô nghĩa này không gì khác ngoài việc xác minh phiên đối với một tên máy chủ cố định. Điểm, nếu có, không được nêu. Không sử dụng định dạng trích dẫn cho văn bản không được trích dẫn, – EJP

Các vấn đề liên quan