验证自签名安全证书,Android

时间:2010-08-26 17:45:45

标签: android https httpclient

使用HttpClient连接到.NET Web服务时,我需要验证自签名证书。任何人都可以将我重定向到任何资源吗?

谢谢,
Teja。

编辑:根据我在发布后所学到的内容,来自http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientCustomSSL.java的以下src代码应该有所帮助 -

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();        
}

现在我的麻烦是生成my.keystore文件并为其创建密码。有什么提示吗?

谢谢, 的Teja。

edit2:更多更新。尝试在mac上使用keytool将其导出到.keystore,无法弄清楚如何做到这一点。它一直说“给定最终块没有正确填充”。

为什么让我跳过如此多的箍? :@在B'Berry和Droid上这很容易:(

0 个答案:

没有答案