我想在我的Android应用中使用自签名证书,而不是依赖证书颁发机构。
我发现this link (thoughcrime.org)有理由这样做,以及一些例子,但无法使其适应我的loopj场景(我没有得到服务器的回答,见下文)。
然后我来到了解决同一问题的其他链接(sproutsocial和Antoine's blog),包括loopj和volley片段。最后我使用了以下代码:
private static SSLSocketFactory getSSLSocketFactory(){
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, "mysecret".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);
}
}
并且,每当我要与AsyncHttpClient
进行连接呼叫时,我都会设置客户端的SSLSocketFactory:
private static AsyncHttpClient client = new AsyncHttpClient();
client.setSSLSocketFactory(getSSLSocketFactory());
创建密钥库时,所有终端输出都与Antoine's blog
中描述的一样尽管如此,我的JsonHttpResponseHandler
未收到任何内容,并且未调用其onSuccess
/ onFailure
方法。
我会很感激你的帮助。 谢谢!
编辑 - This question与我的问题差不多,但没有得到答案