Https Connection Android


I am doing a https post and I'm getting an exception of ssl exception Not trusted server certificate. If i do normal http it is working perfectly fine. Do I have to accept the server certificate somehow?

8/13/2010 4:01:07 PM

Accepted Answer

I'm making a guess, but if you want an actual handshake to occur, you have to let android know of your certificate. If you want to just accept no matter what, then use this pseudo-code to get what you need with the Apache HTTP Client:

SchemeRegistry schemeRegistry = new SchemeRegistry ();

schemeRegistry.register (new Scheme ("http",
    PlainSocketFactory.getSocketFactory (), 80));
schemeRegistry.register (new Scheme ("https",
    new CustomSSLSocketFactory (), 443));

ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager (
    params, schemeRegistry);

return new DefaultHttpClient (cm, params);


public class CustomSSLSocketFactory extends org.apache.http.conn.ssl.SSLSocketFactory
private SSLSocketFactory FACTORY = HttpsURLConnection.getDefaultSSLSocketFactory ();

public CustomSSLSocketFactory ()
        SSLContext context = SSLContext.getInstance ("TLS");
        TrustManager[] tm = new TrustManager[] { new FullX509TrustManager () };
        context.init (null, tm, new SecureRandom ());

        FACTORY = context.getSocketFactory ();
    catch (Exception e)

public Socket createSocket() throws IOException
    return FACTORY.createSocket();

 // TODO: add other methods like createSocket() and getDefaultCipherSuites().
 // Hint: they all just make a call to member FACTORY 

FullX509TrustManager is a class that implements, yet none of the methods actually perform any work, get a sample here.

Good Luck!

5/26/2011 12:52:58 PM

This is what I am doing. It simply doesn't check the certificate anymore.

// always verify the host - dont check for certificate
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
    public boolean verify(String hostname, SSLSession session) {
        return true;

 * Trust every server - dont check for any certificate
private static void trustAllHosts() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public[] getAcceptedIssuers() {
            return new[] {};

        public void checkClientTrusted(X509Certificate[] chain,
                String authType) throws CertificateException {

        public void checkServerTrusted(X509Certificate[] chain,
                String authType) throws CertificateException {
    } };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new;
    } catch (Exception e) {


    HttpURLConnection http = null;

    if (url.getProtocol().toLowerCase().equals("https")) {
        HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
        http = https;
    } else {
        http = (HttpURLConnection) url.openConnection();

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow