任何信息添加該類
public class HttpsTrustManager implements X509TrustManager {
private static TrustManager[] trustManagers;
private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};
@Override
public void checkClientTrusted(
X509Certificate[] x509Certificates, String s)
throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(
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 | KeyManagementException e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context != null ? context.getSocketFactory() : null);
}
}
,並從您的MainActivity與HttpsTrustManager.allowAllSSL();
叫它雖然它不救的方法,但我解決我的問題與此有關。
您確定您正在運行具有使用'hostname1'而不是'oldhostname'的URL的代碼嗎?您的症狀適合您的Java代碼仍在使用'oldhostname'的情況。 – CommonsWare
這可能是一個錯誤的URL的問題,在這種情況下,你會在瀏覽器中得到類似的錯誤。或者這是SNI的問題,請參閱http://stackoverflow.com/questions/5879894/android-ssl-sni-support。 –
是的,我確定這個網址是正確的。我很確定Steffen是對的,前段時間我對SNI/Android進行了很快的瀏覽,並且我明白,對於任何高於2.x的內容,它都是可以的。看起來我誤解了我讀的內容,我會花點時間仔細閱讀鏈接。非常感謝Steffen –