javax.net.ssl.SSLException: Not trusted server certificate

今天写项目遇到:

05-13 02:54:19.636: WARN/System.err(294): javax.net.ssl.SSLException: Not trusted server certificate
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:360)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:168)
at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection$HttpsEngine.connect(HttpsURLConnection.java:398)
at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection.connect(HttpsURLConnection.java:146)
at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:75)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:136)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:90)

at com.ceosoft.acemetrix.wsdl.WSDLClient.MakeCall(WSDLClient.java:130)
at com.ceosoft.acemetrix.wsdl.WSDLClient.InvokeMethod(WSDLClient.java:108)
at com.ceosoft.acemetrix.wsdl.WSDLClient.getDailyTop5(WSDLClient.java:146)
at com.ceosoft.acemetrix.data.manager.DataManager$1.launch(DataManager.java:144)
at com.ceosoft.acemetrix.data.DataTask.doInBackground(DataTask.java:74)
at com.ceosoft.acemetrix.data.DataTask.doInBackground(DataTask.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)


Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:168)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:355)
... 18 more
Caused by: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found.
at org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:149)
at java.security.cert.CertPathValidator.validate(CertPathValidator.java:211)
at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:164)
... 19 more

-----------------------------------------------------------------------------------------------------

找了一些参考资料:

http://stackoverflow.com/questions/995514/https-connection-android#1000205

http://groups.google.com/group/android-developers/browse_thread/thread/62d856cdcfa9f16e/dd59b1998d23a660?lnk=gst&q=SSL+fake+cert#dd59b1998d23a660

------------------------------------------------------------------------------------------------

得到结论:

call allowAllSSL() before you do any SSL communication/call to ksoap2. It will register a new default HostnameVerifier and TrustManager. ksoap2, when doing its SSL communication, will use the default ones and it works like a charm.

-------------------------------------------------------------------------------------------------

解决方法:

在项目中添加类:

public class _FakeX509TrustManager implements X509TrustManager {

private static TrustManager[] trustManagers;
private static final X509Certificate[] _AcceptedIssuers = new
X509Certificate[] {};

@Override
public void checkClientTrusted(X509Certificate[] chain, String
authType) throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String
authType) throws CertificateException {
}

public boolean isClientTrusted(X509Certificate[] chain) {
return true;
}

public boolean isServerTrusted(X509Certificate[] chain) {
return true;
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return _AcceptedIssuers;
}

public static void allowAllSSL() {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
{
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}

});

SSLContext context = null;
if (trustManagers == null) {
trustManagers = new TrustManager[] { new _FakeX509TrustManager() };
}

try {
context = SSLContext.getInstance("TLS");
context.init(null, trustManagers, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}

HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}

}

--------------然后在:

调用:

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{

_FakeX509TrustManager.allowAllSSL() ; // solution: javax.net.ssl.SSLException: Not trusted server certificate
androidHttpTransport.call(soap_action, Envelope);
SoapObject response = (SoapObject)Envelope.getResponse();

return response;
}
catch(Exception e)
{
e.printStackTrace();

}
return null;

你可能感兴趣的:(certificate)