views:

39

answers:

0

Hi,

I need to validate a self-signed certificate when connecting to a .NET web service using HttpClient. Can anyone redirect me to any resources ?

Thanks,
Teja.

Edit: From what I've learned after posting, the following src code from http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java should help -

public final static void main(String[] args) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();

    KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());        
    FileInputStream instream = new FileInputStream(new File("my.keystore")); 
    try {
        trustStore.load(instream, "nopassword".toCharArray());
    } finally {
        instream.close();
    }

    SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
    Scheme sch = new Scheme("https", socketFactory, 443);
    httpclient.getConnectionManager().getSchemeRegistry().register(sch);

    HttpGet httpget = new HttpGet("https://localhost/");

    System.out.println("executing request" + httpget.getRequestLine());

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
    }
    if (entity != null) {
        entity.consumeContent();
    }

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();        
}

Now my trouble is generating the my.keystore file and creating a password for it. Any tips?

Thanks, Teja.

edit2: More updates. Tried using keytool on mac to export it to a .keystore, couldn't figure out how to do it. It keeps saying "Given final block not properly padded".

Why is it making me jump through so many hoops? :@ This was so easy on the B'Berry and the Droid :(