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;

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
javax.net.ssl.SSLException: Unsupported or unrecognized SSL message是一个SSL异常,它表示在进行SSL握手时收到了不支持或无法识别的SSL消息。这可能是由于以下原因导致的: 1. 协议不匹配:客户端和服务器之间的SSL协议版本不匹配。可以尝试升级或降级SSL协议版本来解决此问题。 2. 代理问题:如果在客户端和服务器之间存在代理服务器,可能会导致SSL握手失败。可以尝试绕过代理服务器或配置代理服务器以支持SSL。 3. 证书问题:客户端或服务器的SSL证书可能无效或过期。可以尝试更新证书或使用有效的证书来解决此问题。 4. 密钥问题:客户端或服务器的SSL密钥可能无效或不匹配。可以尝试重新生成密钥或使用正确的密钥来解决此问题。 解决javax.net.ssl.SSLException: Unsupported or unrecognized SSL message的方法包括: 1. 检查SSL协议版本:确保客户端和服务器之间的SSL协议版本匹配。可以尝试升级或降级SSL协议版本来解决此问题。 2. 检查代理设置:如果存在代理服务器,请确保代理服务器正确配置以支持SSL。可以尝试绕过代理服务器或配置代理服务器以支持SSL。 3. 更新证书:检查客户端和服务器的SSL证书是否有效且未过期。如果证书无效或过期,可以尝试更新证书来解决此问题。 4. 重新生成密钥:如果SSL密钥无效或不匹配,可以尝试重新生成密钥或使用正确的密钥来解决此问题。 范例:<<引用:检查SSL协议版本是否匹配。[^1]。引用:检查代理设置是否正确。[^2]。引用:更新SSL证书。[^3]。引用:重新生成SSL密钥。[^4]。 以下是解决javax.net.ssl.SSLException: Unsupported or unrecognized SSL message的方法: 1. 检查SSL协议版本 - 确保客户端和服务器之间的SSL协议版本匹配。 - 可以尝试升级或降级SSL协议版本来解决此问题。 2. 检查代理设置 - 如果存在代理服务器,请确保代理服务器正确配置以支持SSL。 - 可以尝试绕过代理服务器或配置代理服务器以支持SSL。 3. 更新SSL证书[^3] - 检查客户端和服务器的SSL证书是否有效且未过期。 - 如果证书无效或过期,可以尝试更新证书来解决此问题。 4. 重新生成SSL密钥[^4] - 如果SSL密钥无效或不匹配,可以尝试重新生成密钥或使用正确的密钥来解决此问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值