如此处解释:
http://developer.android.com/training/articles/security-ssl.html#WarningsSslSocket
在网站代码的和平中
SocketFactory sf = SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) sf.createSocket("gmail.com", 443);
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
SSLSession s = socket.getSession();
if (!hv.verify("mail.google.com", s)) {
throw new SSLHandshakeException("Expected mail.google.com, "
"found " + s.getPeerPrincipal());
}
我的代码总是在 verfy 中传递 false,然后 getPeerPrincipal() 抛出错误 null
public class Teste extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
try {
SocketFactory socketFactory = SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) socketFactory.createSocket("192.168.13.103", 8181);
HostnameVerifier hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
SSLSession sslSession = sslSocket.getSession();
if (!hostnameVerifier.verify("192.168.13.103", sslSession)) {
System.out.println(sslSession.getPeerHost());
}
} catch (Exception e) {
System.out.println(e);
}
return null;
}
}
最佳答案
据我了解您的代码,您正在尝试连接到 Glassfish 服务器(端口 8181)的 HTTP 套接字,因此没有 SSL/TSL 响应。那么 false 就是好的答案。
关于android - 如何在 android 中直接打开 SSLSocket 到我的 glassfish 默认自签名证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23067928/