HttpClient fails with Handshake Failed in Android 5.0 Lollipop

前端 未结 2 1583
夕颜
夕颜 2020-12-28 18:11

DefaultHttpClient in Android 5.0 Lollipop seems to be broken. It can not set the connection to some sites that were successfully set by previous versions of Android.

2条回答
  •  执念已碎
    2020-12-28 19:10

    I tried changing the cipherSuites in a custom socket factory, but that did not help. In my case, I had to remove the TLSv1.1 and TLSv1.2 protocols from the socket's EnabledProtocols. It appears that some older servers do not handle the protocol negotiation for the new protocols very well. There are various examples out there for creating a custom socket factory, such as How to override the cipherlist sent to the server by Android when using HttpsURLConnection?, and other ones for Apache sockets. That being done, I just called the following AdjustSocket method to remove the protocols.

    private void AdjustSocket(Socket socket)
    {
        String[] protocols = ((SSLSocket) socket).getSSLParameters().getProtocols();
        ArrayList protocolList = new ArrayList(Arrays.asList(protocols));
    
        for (int ii = protocolList.size() - 1; ii >= 0; --ii )
            {
            if ((protocolList.get(ii).contains("TLSv1.1")) || (protocolList.get(ii).contains("TLSv1.2")))
                protocolList.remove(ii);
            }
    
        protocols = protocolList.toArray(new String[protocolList.size()]);
        ((SSLSocket)socket).setEnabledProtocols(protocols);
    }
    

提交回复
热议问题