如何在Android中直接打开我的glassfish默认自签名证书SSLSocket?

时间:2014-04-14 18:51:08

标签: android ssl glassfish

如下所述:

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;

}

}

1 个答案:

答案 0 :(得分:-1)

据我了解您的代码,您正在尝试连接到Glassfish服务器的HTTP套接字(端口8181),因此没有SSL / TSL会响应。然后假是好答案。