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

[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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值