Most visited

Recently visited

Added in API level 1
Deprecated since API level 22

SSLSocketFactory

public class SSLSocketFactory
extends Object implements LayeredSocketFactory

java.lang.Object
   ↳ org.apache.http.conn.ssl.SSLSocketFactory


这个类在API级别22已被弃用。
请改用openConnection() 请访问this webpage了解更多详情。

基于JSSE的分层套接字工厂,用于TLS / SSL连接。

SSLSocketFactory可用于根据可信证书列表验证HTTPS服务器的身份,并使用私钥向HTTPS服务器进行身份验证。

当提供包含一个或多个可信证书的truststore文件时,SSLSocketFactory将启用服务器身份验证。 如果目标HTTPS服务器尝试使用不可信证书对自身进行身份验证,则客户端安全套接字将在SSL会话握手期间拒绝连接。

使用JDK keytool实用程序导入可信证书并生成信任库文件:

     keytool -import -alias "my server cert" -file server.crt -keystore my.truststore
    

SSLSocketFactory将在提供包含私钥/公用证书对的keystore文件时启用客户端身份验证。 如果服务器要求这样做,客户端安全套接字将使用私钥在SSL会话握手期间向目标HTTPS服务器进行身份验证。 目标HTTPS服务器将依次验证客户端提供的证书以建立客户端的真实性

使用以下一系列操作来生成密钥库文件

Summary

Constants

String SSL

String SSLV2

String TLS

Fields

public static final X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER

public static final X509HostnameVerifier BROWSER_COMPATIBLE_HOSTNAME_VERIFIER

public static final X509HostnameVerifier STRICT_HOSTNAME_VERIFIER

Public constructors

SSLSocketFactory(String algorithm, KeyStore keystore, String keystorePassword, KeyStore truststore, SecureRandom random, HostNameResolver nameResolver)
SSLSocketFactory(KeyStore keystore, String keystorePassword, KeyStore truststore)
SSLSocketFactory(KeyStore keystore, String keystorePassword)
SSLSocketFactory(KeyStore truststore)

Public methods

Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort, HttpParams params)

将套接字连接到给定的主机。

Socket createSocket(Socket socket, String host, int port, boolean autoClose)

返回连接到分层到现有套接字上的给定主机的套接字。

Socket createSocket()

创建一个新的未连接的套接字。

X509HostnameVerifier getHostnameVerifier()
static SSLSocketFactory getSocketFactory()

获取SSLProtocolSocketFactory的单例实例。

boolean isSecure(Socket sock)

检查套接字连接是否安全。

void setHostnameVerifier(X509HostnameVerifier hostnameVerifier)

Inherited methods

From class java.lang.Object
From interface org.apache.http.conn.scheme.LayeredSocketFactory
From interface org.apache.http.conn.scheme.SocketFactory

Constants

SSL

Added in API level 1
String SSL

常数值:“SSL”

SSLV2

Added in API level 1
String SSLV2

常数值:“SSLv2”

TLS

Added in API level 1
String TLS

常量值:“TLS”

Fields

ALLOW_ALL_HOSTNAME_VERIFIER

Added in API level 1
X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER

BROWSER_COMPATIBLE_HOSTNAME_VERIFIER

Added in API level 1
X509HostnameVerifier BROWSER_COMPATIBLE_HOSTNAME_VERIFIER

STRICT_HOSTNAME_VERIFIER

Added in API level 1
X509HostnameVerifier STRICT_HOSTNAME_VERIFIER

Public constructors

SSLSocketFactory

Added in API level 1
SSLSocketFactory (String algorithm, 
                KeyStore keystore, 
                String keystorePassword, 
                KeyStore truststore, 
                SecureRandom random, 
                HostNameResolver nameResolver)

Parameters
algorithm String
keystore KeyStore
keystorePassword String
truststore KeyStore
random SecureRandom
nameResolver HostNameResolver
Throws
NoSuchAlgorithmException
KeyManagementException
KeyStoreException
UnrecoverableKeyException

SSLSocketFactory

Added in API level 1
SSLSocketFactory (KeyStore keystore, 
                String keystorePassword, 
                KeyStore truststore)

Parameters
keystore KeyStore
keystorePassword String
truststore KeyStore
Throws
NoSuchAlgorithmException
KeyManagementException
KeyStoreException
UnrecoverableKeyException

SSLSocketFactory

Added in API level 1
SSLSocketFactory (KeyStore keystore, 
                String keystorePassword)

Parameters
keystore KeyStore
keystorePassword String
Throws
NoSuchAlgorithmException
KeyManagementException
KeyStoreException
UnrecoverableKeyException

SSLSocketFactory

Added in API level 1
SSLSocketFactory (KeyStore truststore)

Parameters
truststore KeyStore
Throws
NoSuchAlgorithmException
KeyManagementException
KeyStoreException
UnrecoverableKeyException

Public methods

connectSocket

Added in API level 1
Socket connectSocket (Socket sock, 
                String host, 
                int port, 
                InetAddress localAddress, 
                int localPort, 
                HttpParams params)

将套接字连接到给定的主机。

Parameters
sock Socket: the socket to connect, as obtained from createSocket. null indicates that a new socket should be created and connected.
host String: the host to connect to
port int: the port to connect to on the host
localAddress InetAddress: the local address to bind the socket to, or null for any
localPort int: the port on the local machine, 0 or a negative number for any
params HttpParams: additional parameters for connecting
Returns
Socket the connected socket. The returned object may be different from the sock argument if this factory supports a layered protocol.
Throws
IOException

createSocket

Added in API level 1
Socket createSocket (Socket socket, 
                String host, 
                int port, 
                boolean autoClose)

返回连接到分层到现有套接字上的给定主机的套接字。 主要用于通过代理创建安全套接字。

Parameters
socket Socket: the existing socket
host String: the host name/IP
port int: the port on the host
autoClose boolean: a flag for closing the underling socket when the created socket is closed
Returns
Socket Socket a new socket
Throws
IOException
UnknownHostException

createSocket

Added in API level 1
Socket createSocket ()

创建一个新的未连接的套接字。 该套接字应该随后传递给connectSocket

Returns
Socket a new socket
Throws
IOException

getHostnameVerifier

Added in API level 1
X509HostnameVerifier getHostnameVerifier ()

Returns
X509HostnameVerifier

getSocketFactory

Added in API level 1
SSLSocketFactory getSocketFactory ()

获取SSLProtocolSocketFactory的单例实例。

Returns
SSLSocketFactory a SSLProtocolSocketFactory

isSecure

Added in API level 1
boolean isSecure (Socket sock)

检查套接字连接是否安全。 该工厂创建TLS / SSL套接字连接,默认情况下,这些连接被认为是安全的。
派生类可以重写此方法来执行运行时检查,例如基于密码套件。

Parameters
sock Socket: the connected socket
Returns
boolean true
Throws
IllegalArgumentException if the argument is invalid

setHostnameVerifier

Added in API level 1
void setHostnameVerifier (X509HostnameVerifier hostnameVerifier)

Parameters
hostnameVerifier X509HostnameVerifier

Hooray!