通过排球呼叫https Api时面临错误

时间:2017-07-10 13:37:06

标签: android ssl

我有3个服务器设置,代码相同。

  1. 没有SSL的服务器1 :工作正常。
  2. 服务器2在生产服务器上使用SSL (授权证书):工作正常。
  3. 在测试服务器上使用SSL的服务器3 (授权证书相同的认证 作为服务器2):但它不起作用。它引发了一个例外如下。
  4. 错误:

     onErrorResponse: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
                                    in.chsone.member.exceptions.UnhandledException: error.networkResponse is null.
                                        at in.chsone.member.network.NetworkResponseHandler.onErrorResponse(NetworkResponseHandler.java:181)
                                        at com.android.volley.Request.deliverError(Request.java:598)
                                        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
                                        at android.os.Handler.handleCallback(Handler.java:739)
                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                        at android.os.Looper.loop(Looper.java:148)
                                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                                        at java.lang.reflect.Method.invoke(Native Method)
                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    

1 个答案:

答案 0 :(得分:2)

public class HttpsTrustManager implements X509TrustManager {

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

@Override
public void checkClientTrusted(
        java.security.cert.X509Certificate[] x509Certificates, String s)
        throws java.security.cert.CertificateException {

}

@Override
public void checkServerTrusted(
        java.security.cert.X509Certificate[] x509Certificates, String s)
        throws java.security.cert.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 arg0, SSLSession arg1) {
            return true;
        }

    });

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

    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());
}

}

在调用api之前调用下面的行

HttpsTrustManager.allowAllSSL();