Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android测试中的SSLHandshakeException_Java_Android_Ssl_Testing - Fatal编程技术网

Java Android测试中的SSLHandshakeException

Java Android测试中的SSLHandshakeException,java,android,ssl,testing,Java,Android,Ssl,Testing,我在android测试中遇到SSL问题。测试看起来像 @Test public void requestSettings() throws Exception { TestSubscriberBase<Settings> subscriberBase = TestSubscribers.mustHaveResult(); CommonApi.requestSettings(true).subscribe(subscriberBase); System.out.

我在android测试中遇到SSL问题。测试看起来像

@Test
public void requestSettings() throws Exception {
    TestSubscriberBase<Settings> subscriberBase = TestSubscribers.mustHaveResult();
    CommonApi.requestSettings(true).subscribe(subscriberBase);
    System.out.println(subscriberBase.result.text);
}
我的尝试:

  • 信任所有证书

     TrustManager[] trustAllCerts = new TrustManager[] {
        new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
            public void checkClientTrusted(X509Certificate[] certs, String authType) {}
            public void checkServerTrusted(X509Certificate[] certs, String authType) {}
        }};
    
     HostnameVerifier hv = new HostnameVerifier() {
         public boolean verify(String hostname, SSLSession session) { return true; }
     };
    
    try {
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new SecureRandom());
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
      HttpsURLConnection.setDefaultHostnameVerifier(hv);
    } catch (Exception e) {
    }
    
  • 设置系统属性Dcom.sun.net.ssl.checkRevocation=false
    system.getProperties().setProperty(“Dcom.sun.net.ssl.checkRevocation”,“false”)

  • 那么我可以做些什么来避免SSLHandshakeException?


    p.S.我不想添加证书,因为有很多测试url,所以我没有太多时间为每个url添加证书。

    发现问题。

    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    
    看起来这两种方法不适用于android测试环境,但适用于android应用程序

    因此,我只是传递了real
    HttpsUrlConnection
    实例,并设置了
    SSLSocketFactory
    HostNameVerifier

    public void disableSSLinitDisablingSSL(HttpsURLConnection httpsURLConnection {
      TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                public void checkServerTrusted(X509Certificate[] certs, String authType) {}
            }};
    
      HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) { return true; }
      };
    
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new SecureRandom());
      httpsURLConnection.setSSLSocketFactory(sc.getSocketFactory());
      httpsURLConnection.setHostnameVerifier(hv);
    }
    

    尝试了这个,但没有成功(使用API 21 emulator测试)
    public void disableSSLinitDisablingSSL(HttpsURLConnection httpsURLConnection {
      TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                public void checkServerTrusted(X509Certificate[] certs, String authType) {}
            }};
    
      HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) { return true; }
      };
    
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new SecureRandom());
      httpsURLConnection.setSSLSocketFactory(sc.getSocketFactory());
      httpsURLConnection.setHostnameVerifier(hv);
    }