Android Https web service communication (SSL / TLS 1.2)

后端 未结 2 842
粉色の甜心
粉色の甜心 2021-02-09 06:27

In my Android application I\'ve got to communicate with a https web service and read the response.

I\'ve informed the server configured SSL with

2条回答
  •  野性不改
    2021-02-09 07:15

    Here is a Kotlin version of the answer from Robert, which fixed the problem in my case:

    import java.net.InetAddress
    import java.net.Socket
    import javax.net.ssl.SSLSocket
    import javax.net.ssl.SSLSocketFactory
    
    class MySSLSocketFactory(socket_factory :SSLSocketFactory):
          SSLSocketFactory()
    {
        val sslSocketFactory :SSLSocketFactory
    
        init
        {
            this.sslSocketFactory = socket_factory
        }
    
        override fun getDefaultCipherSuites(): Array
        {
            return this.sslSocketFactory.getDefaultCipherSuites()
        }
    
        override fun createSocket(socket: Socket?, host: String?, port: Int, autoclose: Boolean): Socket
        {
            val socket: SSLSocket = this.sslSocketFactory.createSocket(socket, host, port, autoclose) as SSLSocket
            socket.setEnabledProtocols(arrayOf("TLSv1.2"))
            return socket
        }
    
        override fun createSocket(host: String?, port: Int): Socket
        {
            val socket: SSLSocket = this.sslSocketFactory.createSocket(host, port) as SSLSocket
            socket.setEnabledProtocols(arrayOf("TLSv1.2"))
            return socket
        }
    
        override fun createSocket(host: String?, port: Int, local_host: InetAddress?, local_port: Int): Socket {
            val socket = sslSocketFactory.createSocket(host, port, local_host, local_port) as SSLSocket
            socket.enabledProtocols = arrayOf("TLSv1.2")
            return socket
        }
    
        override fun createSocket(host: InetAddress?, port: Int): Socket
        {
            val socket = sslSocketFactory.createSocket(host, port) as SSLSocket
            socket.enabledProtocols = arrayOf("TLSv1.2")
            return socket
        }
    
        override fun createSocket(address: InetAddress?, port: Int, local_address: InetAddress?, local_port: Int): Socket
        {
            val socket = sslSocketFactory.createSocket(address, port, local_address, local_port) as SSLSocket
            socket.enabledProtocols = arrayOf("TLSv1.2")
            return socket
        }
    
        override fun getSupportedCipherSuites(): Array
        {
            return this.sslSocketFactory.getSupportedCipherSuites()
        }
    }
    

提交回复
热议问题