Receive & Validate certificate from server HTTPS - android

前端 未结 1 1887
一向
一向 2021-02-06 05:54

I am calling web service from my android client via https. I got to validate the certificate receive from server side. How do I do that ? At present this is my code that I use t

相关标签:
1条回答
  • 2021-02-06 06:26

    Bob Lee wrote a nice blog post on how using SSL certificates with Android. I think it is applicable to your case: http://blog.crazybob.org/2010/02/android-trusting-ssl-certificates.html

    You just have to create a KeyStore containing your self-signed certificate and use the custom HttpClient implementation described in that post.


    UPDATE:

    Host name validation can be customizez by setting a custom X509HostnameVerifier on the SSLSocketFactory. Some implementations are already available in android: AllowAllHostnameVerifier, BrowserCompatHostnameVerifier, StrictHostnameVerifier

    /* ... */
    public class MyHostnameVerifier extends AbstractVerifier {
      boolean verify(String hostname, SSLSession session) {
        X509Certificate[] chain = session.getPeerCertificateChain();
        /* made some checks... */
        return checked;
      }
    }
    sslSocketFactory.setHostnameVerifier(new MyHostnameVerifier());
    
    0 讨论(0)
提交回复
热议问题