本文整理汇总了Java中android.net.SSLCertificateSocketFactory.setHostname方法的典型用法代码示例。如果您正苦于以下问题:Java SSLCertificateSocketFactory.setHostname方法的具体用法?Java SSLCertificateSocketFactory.setHostname怎么用?Java SSLCertificateSocketFactory.setHostname使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.net.SSLCertificateSocketFactory
的用法示例。
在下文中一共展示了SSLCertificateSocketFactory.setHostname方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setSniHost
import android.net.SSLCertificateSocketFactory; //导入方法依赖的package包/类
public static void setSniHost(SSLSocketFactory factory, SSLSocket socket, String hostname) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 &&
factory instanceof android.net.SSLCertificateSocketFactory) {
SSLCertificateSocketFactory sslCertificateSocketFactory = (SSLCertificateSocketFactory) factory;
sslCertificateSocketFactory.setHostname(socket, hostname);
} else {
setHostnameViaReflection(socket, hostname);
}
}
示例2: createSocket
import android.net.SSLCertificateSocketFactory; //导入方法依赖的package包/类
@Override
public Socket createSocket(Socket plainSocket, String host, int port, boolean autoClose) throws IOException {
if (autoClose) {
// we don't need the plainSocket
plainSocket.close();
}
// create and connect SSL socket, but don't do hostname/certificate verification yet
SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
// enable TLSv1.1/1.2 if available
// (see https://github.com/rfc2822/davdroid/issues/229)
ssl.setEnabledProtocols(ssl.getSupportedProtocols());
// set up SNI before the handshake
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sslSocketFactory.setHostname(ssl, host);
} else {
try {
java.lang.reflect.Method setHostnameMethod = ssl.getClass().getMethod("setHostname", String.class);
setHostnameMethod.invoke(ssl, host);
} catch (Exception ignored) {
}
}
// verify hostname and certificate
SSLSession session = ssl.getSession();
if (!hostnameVerifier.verify(host, session))
throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);
return ssl;
}
示例3: createSocket
import android.net.SSLCertificateSocketFactory; //导入方法依赖的package包/类
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public Socket createSocket(Socket plainSocket, String host, int port, boolean autoClose) throws IOException {
if (autoClose) {
// we don't need the plainSocket
plainSocket.close();
}
SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
// create and connect SSL socket, but don't do hostname/certificate verification yet
SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
// enable TLSv1.1/1.2 if available
ssl.setEnabledProtocols(ssl.getSupportedProtocols());
// set up SNI before the handshake
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sslSocketFactory.setHostname(ssl, host);
} else {
try {
java.lang.reflect.Method setHostnameMethod = ssl.getClass().getMethod("setHostname", String.class);
setHostnameMethod.invoke(ssl, host);
} catch (Exception e) {
Log.d(TlsSniSocketFactory.class.getSimpleName(), "SNI not usable: " + e);
}
}
// verify hostname and certificate
SSLSession session = ssl.getSession();
if (!hostnameVerifier.verify(host, session)) {
throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);
}
return ssl;
}
示例4: createSocket
import android.net.SSLCertificateSocketFactory; //导入方法依赖的package包/类
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public Socket createSocket(Socket plainSocket, String host, int port, boolean autoClose) throws IOException {
if (autoClose) {
// we don't need the plainSocket
plainSocket.close();
}
SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
// For self-signed certificates use a custom trust manager
sslSocketFactory.setTrustManagers(new TrustManager[]{new IgnoreSSLTrustManager()});
// create and connect SSL socket, but don't do hostname/certificate verification yet
SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
// enable TLSv1.1/1.2 if available
ssl.setEnabledProtocols(ssl.getSupportedProtocols());
// set up SNI before the handshake
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sslSocketFactory.setHostname(ssl, host);
} else {
try {
java.lang.reflect.Method setHostnameMethod = ssl.getClass().getMethod("setHostname", String.class);
setHostnameMethod.invoke(ssl, host);
} catch (Exception e) {
throw new IOException("SNI not usable: " + e, e);
}
}
return ssl;
}
示例5: createSocket
import android.net.SSLCertificateSocketFactory; //导入方法依赖的package包/类
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public Socket createSocket(Socket plainSocket, String host, int port, boolean autoClose) throws IOException {
if (autoClose) {
// we don't need the plainSocket
plainSocket.close();
}
SSLCertificateSocketFactory sslSocketFactory =
(SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
// For self-signed certificates use a custom trust manager
if (acceptAllCertificates) {
sslSocketFactory.setTrustManagers(new TrustManager[]{new IgnoreSSLTrustManager()});
} else if (selfSignedCertificateKey != null) {
sslSocketFactory.setTrustManagers(new TrustManager[]{new SelfSignedTrustManager(selfSignedCertificateKey)});
}
// create and connect SSL socket, but don't do hostname/certificate verification yet
SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
// enable TLSv1.1/1.2 if available
ssl.setEnabledProtocols(ssl.getSupportedProtocols());
// set up SNI before the handshake
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sslSocketFactory.setHostname(ssl, host);
} else {
try {
java.lang.reflect.Method setHostnameMethod = ssl.getClass().getMethod("setHostname", String.class);
setHostnameMethod.invoke(ssl, host);
} catch (Exception e) {
Log.d(TlsSniSocketFactory.class.getSimpleName(), "SNI not usable: " + e);
}
}
// verify hostname and certificate
SSLSession session = ssl.getSession();
if (!(acceptAllCertificates || selfSignedCertificateKey != null) && !hostnameVerifier.verify(host, session)) {
throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);
}
/*DLog.d(TlsSniSocketFactory.class.getSimpleName(),
"Established " + session.getProtocol() + " connection with " + session.getPeerHost() +
" using " + session.getCipherSuite());*/
return ssl;
}
示例6: createSocket
import android.net.SSLCertificateSocketFactory; //导入方法依赖的package包/类
@Override
public Socket createSocket (final Socket plainSocket, final String host, final int port, final boolean autoClose) throws IOException, UnknownHostException {
// we don't need the plainSocket
if (autoClose) plainSocket.close();
// create and connect SSL socket, but don't do hostname/certificate verification yet.
final SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
sslSocketFactory.setTrustManagers(this.trustManager);
final SSLSocket sock = (SSLSocket) sslSocketFactory.createSocket(InetAddress.getByName(host), port);
// Protocols...
final List<String> protocols = new ArrayList<String>();
for (final String protocol : sock.getSupportedProtocols()) {
if (!protocol.toUpperCase(Locale.ENGLISH).contains("SSL")) protocols.add(protocol);
}
sock.setEnabledProtocols(protocols.toArray(new String[0]));
// Ciphers...
final HashSet<String> ciphers = new HashSet<String>(ALLOWED_CIPHERS);
ciphers.retainAll(Arrays.asList(sock.getSupportedCipherSuites()));
ciphers.addAll(new HashSet<String>(Arrays.asList(sock.getEnabledCipherSuites()))); // All all already enabled ones for compatibility.
sock.setEnabledCipherSuites(ciphers.toArray(new String[0]));
// set up SNI before the handshake.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sslSocketFactory.setHostname(sock, host);
}
else { // This hack seems to work on my 4.0.4 tablet.
try {
final java.lang.reflect.Method setHostnameMethod = sock.getClass().getMethod("setHostname", String.class);
setHostnameMethod.invoke(sock, host);
}
catch (final Exception e) {
LOG.w("SNI not useable: %s", ExcpetionHelper.causeTrace(e));
}
}
// verify hostname and certificate.
final SSLSession session = sock.getSession();
if (!HOSTNAME_VERIFIER.verify(host, session)) throw new SSLPeerUnverifiedException("Cannot verify hostname: " + host);
LOG.i("Connected %s %s %s.", session.getPeerHost(), session.getProtocol(), session.getCipherSuite());
return sock;
}