我正在尝试通过SSL端口443连接到我的服务器而没有证书。
我收到了错误:
javax.net.ssl.SSLException:不受信任的服务器证书
阅读其他问题来解决问题,以下代码应该可以工作,但我仍然收到错误消息。我能做错什么?
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
httpclient = new DefaultHttpClient(mgr, client.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
HttpGet httpget = new HttpGet(this.requestedURL);
httpget.addHeader(new BasicScheme().authenticate(creds, httpget));
try
{
response = httpclient.execute(httpget);
}
catch(java.lang.Throwable t) {}
答案 0 :(得分:0)
您的客户端信任库不信任服务器证书。它可能是一个自签名证书,因此您需要将其导入客户端信任库。或者由CA签名。忽略服务器证书并不安全,您也可以根本不使用HTTPS。
答案 1 :(得分:0)
尝试所有其他解决方案后,Android 2.2 +需要特殊代码。这工作