Android https connection with self-signed certificate gives org.apache.http.conn.ConnectTimeoutException
I am making a https connection to a client server which has a self-signed certificate by following [these instructions][1].

If the server is my local machine with a self-signed certificate it worked perfectly.

But now the server is at a remote location to which i am connecting.Now i am getting following exception

    WARN/System.err(355): org.apache.http.conn.ConnectTimeoutException: Connect to server timed out
    WARN/System.err(355):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
    WARN/System.err(355):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
    WARN/System.err(355):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    WARN/System.err(355):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    WARN/System.err(355):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
    WARN/System.err(355):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    WARN/System.err(355):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    WARN/System.err(355):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)


I have the right certificate of the server to which i am connecting.

My httpclient is like the following

    public class MyHttpClient extends DefaultHttpClient {
    	
    	/** The time it takes for our client to timeout */
        public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
        public static final int SOCKET_TIMEOUT = 50 * 1000; // milliseconds
        
    	final Context context;
    	
    	public MyHttpClient(Context context) {
    		 this.context = context;
    	}
    	
    	@Override
    	protected ClientConnectionManager createClientConnectionManager() {
    		
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 8080));
            // Register for port 443 our SSLSocketFactory with our keystore
            // to the ConnectionManager
            registry.register(new Scheme("https", newSslSocketFactory(), 8443));
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
            return new SingleClientConnManager(params, registry);
        }
     
        private SSLSocketFactory newSslSocketFactory() {
            try {
                // Get an instance of the Bouncy Castle KeyStore format
                KeyStore trusted = KeyStore.getInstance("BKS");
                // Get the raw resource, which contains the keystore with
                // your trusted certificates (root and any intermediate certs)
                InputStream in = context.getResources().openRawResource(R.raw.mykeystore);
                try {
                    // Initialize the keystore with the provided trusted certificates
                    // Also provide the password of the keystore
                    trusted.load(in, "password".toCharArray());
                } finally {
                    in.close();
                }
                // Pass the keystore to the SSLSocketFactory. The factory is responsible
                // for the verification of the server certificate.
                SSLSocketFactory sf = new SSLSocketFactory(trusted);
                // Hostname verification from certificate
                // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
                sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
                return sf;
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        }
    
    }

And i am connecting like this

    DefaultHttpClient client = new MyHttpClient(getApplicationContext());
       	    		 try {        
       	    	            HttpPost request = new HttpPost(url);
       	    	            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
       	    	            request.setEntity(formEntity);
       	    	            HttpResponse resp = client.execute(request);
       	    	            in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
       	    	           
       	    	            StringBuffer sb = new StringBuffer("");
       	    	            String line = "";
       	    	            String NL = System.getProperty("line.separator");
       	    	            while ((line = in.readLine()) != null) {
       	    	                sb.append(line + NL);
       	    	            }
       	    	            in.close();
       	    	           
       	    	            response = sb.toString();
       	    	        } finally {
       	    	            if (in != null) {
       	    	                try {
       	    	                    in.close();
       	    	                } catch (IOException e) {
       	    	                    e.printStackTrace();
       	    	                }
       	    	            }
       	    	        }

Please somebody provide me a quick solution as it is Time critical.
Thanks in advance.

  [1]: http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/
Android https connection with self-signed certificate gives org.apache.http.conn.ConnectTimeoutException
===

I am making a https connection to a client server which has a self-signed certificate by following [these instructions][1].

If the server is my local machine with a self-signed certificate it worked perfectly.

But now the server is at a remote location to which i am connecting.Now i am getting following exception

    WARN/System.err(355): org.apache.http.conn.ConnectTimeoutException: Connect to server timed out
    WARN/System.err(355):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
    WARN/System.err(355):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
    WARN/System.err(355):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    WARN/System.err(355):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    WARN/System.err(355):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
    WARN/System.err(355):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    WARN/System.err(355):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    WARN/System.err(355):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)


I have the right certificate of the server to which i am connecting.

My httpclient is like the following

    public class MyHttpClient extends DefaultHttpClient {
    	
    	/** The time it takes for our client to timeout */
        public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
        public static final int SOCKET_TIMEOUT = 50 * 1000; // milliseconds
        
    	final Context context;
    	
    	public MyHttpClient(Context context) {
    		 this.context = context;
    	}
    	
    	@Override
    	protected ClientConnectionManager createClientConnectionManager() {
    		
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 8080));
            // Register for port 443 our SSLSocketFactory with our keystore
            // to the ConnectionManager
            registry.register(new Scheme("https", newSslSocketFactory(), 8443));
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
            return new SingleClientConnManager(params, registry);
        }
     
        private SSLSocketFactory newSslSocketFactory() {
            try {
                // Get an instance of the Bouncy Castle KeyStore format
                KeyStore trusted = KeyStore.getInstance("BKS");
                // Get the raw resource, which contains the keystore with
                // your trusted certificates (root and any intermediate certs)
                InputStream in = context.getResources().openRawResource(R.raw.mykeystore);
                try {
                    // Initialize the keystore with the provided trusted certificates
                    // Also provide the password of the keystore
                    trusted.load(in, "password".toCharArray());
                } finally {
                    in.close();
                }
                // Pass the keystore to the SSLSocketFactory. The factory is responsible
                // for the verification of the server certificate.
                SSLSocketFactory sf = new SSLSocketFactory(trusted);
                // Hostname verification from certificate
                // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
                sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
                return sf;
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        }
    
    }

And i am connecting like this

    DefaultHttpClient client = new MyHttpClient(getApplicationContext());
       	    		 try {        
       	    	            HttpPost request = new HttpPost(url);
       	    	            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
       	    	            request.setEntity(formEntity);
       	    	            HttpResponse resp = client.execute(request);
       	    	            in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
       	    	           
       	    	            StringBuffer sb = new StringBuffer("");
       	    	            String line = "";
       	    	            String NL = System.getProperty("line.separator");
       	    	            while ((line = in.readLine()) != null) {
       	    	                sb.append(line + NL);
       	    	            }
       	    	            in.close();
       	    	           
       	    	            response = sb.toString();
       	    	        } finally {
       	    	            if (in != null) {
       	    	                try {
       	    	                    in.close();
       	    	                } catch (IOException e) {
       	    	                    e.printStackTrace();
       	    	                }
       	    	            }
       	    	        }

Please somebody provide me a quick solution as it is Time critical.
Thanks in advance.

  [1]: http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/