10/16/2014

Simplest way to connect remote Server using any private certificate (.pfx , .cer) file is very easy in Windows machine.

First step is to import the certificate in Windows.
Double click the certificate  you get below screen->next->next->give the right certificate Password  ->click till get the completing the certificate Wizard ->finish
Note: This is a one time job.



 


















Than just use the below method to get the client.  getHttpClientOnlyWindowsApp()

Below line  create the right keystore for you.  If we are going to use only WINDOWS than we don't need to create keystore as we did in other blog example. Below method create the 
KeyStore keyStore = KeyStore.getInstance("WINDOWS-MY");

Note: This works only for windows machine. If you are using Linux server follow other example.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
protected  DefaultHttpClient getHttpClientOnlyWindowsApp() throws Exception{
                    
                    
                     final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                           @Override
                           public void checkClientTrusted(final X509Certificate[] chain,
                                         final String authType) {
                           }
      
                           @Override
                           public void checkServerTrusted(final X509Certificate[] chain,
                                         final String authType) {
                           }
      
                           @Override
                           public X509Certificate[] getAcceptedIssuers() {
                                  return null;
                           }
                     } };
             
                     SSLContext context = SSLContext.getInstance("SSL");
               KeyManagerFactory keyFac = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
               KeyStore keyStore = KeyStore.getInstance("WINDOWS-MY");
               keyStore.load(null, null);
               keyFac.init(keyStore, null);
              context.init(keyFac.getKeyManagers(), trustAllCerts, null);
               SSLSocketFactory sslsf = new SSLSocketFactory(context);
                     Scheme sch = new Scheme("https", 443, sslsf);
                     DefaultHttpClient httpclient = new DefaultHttpClient();
                     httpclient.getConnectionManager().getSchemeRegistry().register(sch);
                     return httpclient;

              }

1 comment: