Handshake fail on Lollipop

2019-04-02 10:38发布

问题:

I'm trying to do a HTTP POST but I get two different errors:

javax.net.ssl.SSLHandshakeException: Handshake failed
net.ssl.SSLPeerUnverifiedException: No peer certificate

I fixed the No peer certificate error through a workaround as specified here: https://stackoverflow.com/a/4837230/4254419

but while that fixes the error, it throws a new error instead, which is Handshake failed

I know it's not safe and I don't care, it's not for production so I care less about security. Is there a fix for this issue?

回答1:

I had the same problem. I found a link https://code.google.com/p/android/issues/detail?id=88313 where I found a code:

public class MySSLSocketFactory extends SSLSocketFactory {
        SSLContext sslContext = SSLContext.getInstance("TLS");

        ...

        @Override
        public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
            final SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
            sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());
            return sslSocket;
        }

        @Override
        public Socket createSocket() throws IOException {
            final SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket();
            sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());
            return sslSocket;
        }
}

You can use code of MySSLSocketFactory from the link you've provided but you need to override two methods createSocket as I've wrote above. Also it's not the best solution you can have some security issue later, because for connection it can use some old cipher algorithm.

Hope this helps.