Java Android测试中的SSLHandshakeException
我在android测试中遇到SSL问题。测试看起来像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.
@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) {
}
system.getProperties().setProperty(“Dcom.sun.net.ssl.checkRevocation”,“false”)代码>
p.S.我不想添加证书,因为有很多测试url,所以我没有太多时间为每个url添加证书。发现问题。
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
看起来这两种方法不适用于android测试环境,但适用于android应用程序
因此,我只是传递了realHttpsUrlConnection
实例,并设置了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);
}