Handshake fail on Lollipop

谁都会走 提交于 2019-12-05 07:00:35

问题


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.



来源:https://stackoverflow.com/questions/29150400/handshake-fail-on-lollipop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!