[b][color=blue]使用SSL方式,登陆时,报错javax.net.ssl.SSLException: Not trusted server certificate
登陆的时候调用的是webservice,网上终于找到了合适的解决方案,项目现在运行OK
以下为解决方法:
第一步,在项目中添加FakeX509TrustManager 类:
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());
}
}
第二步,在程序里面调用WEBSERVICE的地方
调用:
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{
[/color][color=red] FakeX509TrustManager.allowAllSSL() ; // solution: javax.net.ssl.SSLException: Not trusted server certificate [/color]
[color=blue] androidHttpTransport.call(soap_action, Envelope);
SoapObject response = (SoapObject)Envelope.getResponse();
return response;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
[/color][color=red]
得到结论:
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.
[/color][/b]
登陆的时候调用的是webservice,网上终于找到了合适的解决方案,项目现在运行OK
以下为解决方法:
第一步,在项目中添加FakeX509TrustManager 类:
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());
}
}
第二步,在程序里面调用WEBSERVICE的地方
调用:
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{
[/color][color=red] FakeX509TrustManager.allowAllSSL() ; // solution: javax.net.ssl.SSLException: Not trusted server certificate [/color]
[color=blue] androidHttpTransport.call(soap_action, Envelope);
SoapObject response = (SoapObject)Envelope.getResponse();
return response;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
[/color][color=red]
得到结论:
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.
[/color][/b]