2013-04-04 41 views
5

tôi đã gọi thành công dịch vụ nghỉ ngơi trong Android bằng http.cách gọi dịch vụ nghỉ ngơi an toàn https từ android bằng cách sử dụng httpclient

Nhưng bây giờ tôi muốn gọi dịch vụ an toàn HTTPS trong android Tôi đang sử dụng API HTTPmime của apache để gọi dịch vụ nghỉ ngơi.

Hãy bất cứ ai có thể giúp tôi để gọi dịch vụ còn lại an toàn trong android

+0

Bạn có gặp bất kỳ vấn đề nào mà tôi đoán bạn không nên, nếu có thì sao? – Prateek

+0

Những gì bạn đã thử? –

+0

Tôi đã cố gắng liên kết này http://stackoverflow.com/questions/2864016/httpclient-ssl-certificate-on-android nhưng tôi không thể hiểu được làm thế nào để sử dụng SSL này với HttpClient để gọi dịch vụ của tôi –

Trả lời

0

đây là mẫu mã mà bạn có thể sử dụng để bỏ qua lỗi chứng chỉ SSL tạo ra một lớp java mới có tên SimpleSSLSocketFactory

public class SimpleSSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactory { 
    private SSLSocketFactory sslFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); 

    public SimpleSSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, 
      UnrecoverableKeyException { 
     super(null); 

     try { 
      SSLContext context = SSLContext.getInstance("TLS"); 

      // Create a trust manager that does not validate certificate chains and simply 
      // accept all type of certificates 
      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 { 
       } 
      } }; 

      // Initialize the socket factory 
      context.init(null, trustAllCerts, new SecureRandom()); 
      sslFactory = context.getSocketFactory(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { 
     return sslFactory.createSocket(socket, host, port, autoClose); 
    } 

    @Override 
    public Socket createSocket() throws IOException { 
     return sslFactory.createSocket(); 
    } 
} 

và sử dụng httpclient sau thay vì ứng dụng khách mặc định

// Setup a custom SSL Factory object which simply ignore the certificates 
    // validation and accept all type of self signed certificates 
    SSLSocketFactory sslFactory = new SimpleSSLSocketFactory(null); 
    sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

    // Enable HTTP parameters 
    HttpParams paramsSecure = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(paramsSecure, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(paramsSecure, HTTP.UTF_8); 

    // Register the HTTP and HTTPS Protocols. For HTTPS, register our custom SSL Factory object. 
    SchemeRegistry registry = new SchemeRegistry(); 
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    registry.register(new Scheme("https", sslFactory, 443)); 

    // Create a new connection manager using the newly created registry and then create a new HTTP client 
    // using this connection manager 
    ClientConnectionManager ccm = new ThreadSafeClientConnManager(paramsSecure, registry); 
    HttpClient httpclient = new DefaultHttpClient(ccm, paramsSecure); 

chúc mừng !!

+0

Sourabh thanx cho trả lời của bạn ..... nhưng bây giờ là cung cấp cho "500 lỗi máy chủ nội bộ" html như trả lời –

+0

mã trên đây không làm việc cho tôi. –

+0

Bạn có đang chạy dịch vụ này trên máy chủ cục bộ của mình không? – Prateek

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