표준 대화 상자가 없기 때문에 실제로 HttpClient의 기본 동작은 안드로이드 신뢰할 수있는 인증서 저장소의 일부인 인증서 만 받아들이는 것입니다.
자신의 HttpClient 인스턴스와 연결할 트러스트 관리자를 직접 구축하여이 작업을 수행 할 수 있습니다. 이것은 다음과 같이 보일 것입니다 :
public class PromptUserTrustManager implements X509TrustManager
{
private AcceptUserSelectedCertsTrustManager(ValidateCertificateCallback callback) throws NoSuchAlgorithmException, KeyStoreException
{
KeyStore keyStore = null;
TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
factory.init(keyStore);
TrustManager [] trustmanagers = factory.getTrustManagers();
m_standardTrustManager = (X509TrustManager) trustmanagers[0];
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
{
}
// This is where you check the server cert and make the determination
@Override
public void checkServerTrusted(X509Certificate[] certChain, String authType)throws CertificateException
{
try
{
m_standardTrustManager.checkServerTrusted(certChain,authType);
}
catch(CertificateException e)
{
// Cert isn't trusted - popup the error here. You'll need to
// make sure you switch to the UI thread since here you're on a network thread
if(!userAcceptsCert(certChain))
{
throw e;
}
}
}
}
그래서 기본적으로 당신이 무엇을 당신이이 인증서를 신뢰 여부 플랫폼을 물어 checkServerTrusted의 콜백입니다. 그렇지 않은 경우, 트러스트 매니저를 호출하면 (자) 예외가 슬로우됩니다. 그런 다음 사용자에게 수행 할 작업을 묻는 메시지를 표시 할 수 있습니다.
onReceivedSslError()를 사용하여 WebView에서 동일한 작업을 수행 할 수 있습니다. 사용자는 원하는 경우 계속 진행할 수있는 동일한 경고를 표시 할 수 있습니다.
출처
2012-09-13 17:15:59
Lee