在我的Android应用程序中,我想与网站(例如Google)建立连接,以明确设置要使用的协议(例如TLSv1.1)。
该应用仅受API版本> 20支持。以下是我使用的代码:
URL url = new URL(website);
SSLContext sslContext = SSLContext.getInstance(protocol);
sslContext.init(/*keyManagers*/null, /*trustManagers*/null, /*new SecureRandom()*/null); // simple here
SSLSocketFactory a = sslContext.getSocketFactory();
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(new SecureSSLSocketFactory(sslContext.getSocketFactory(),protocol));
int responseCode = makeGetRequest(conn);
其中的SecureSSLSocketFactory如下:
import android.util.Log;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.net.ssl.HandshakeCompletedEvent;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
class SecureSSLSocketFactory extends SSLSocketFactory {
SSLSocketFactory factory = null;
String defaultProtocol = "SSLv3";
SecureSSLSocketFactory(SSLSocketFactory factory) {
this.factory = factory;
}
SecureSSLSocketFactory(SSLSocketFactory factory, String protocol) {
this.factory = factory;
defaultProtocol = protocol;
}
@Override
public Socket createSocket(Socket s, String host, int port,
boolean autoClose) throws IOException {
Socket skt = factory.createSocket(s, host, port, autoClose);
return customizeSSLSocket(skt);
}
@Override
public String[] getDefaultCipherSuites() {
return factory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return factory.getSupportedCipherSuites();
}
@Override
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
SSLSocket skt = (SSLSocket) factory.createSocket(host, port);
skt.setEnabledProtocols(new String [] {defaultProtocol});
return customizeSSLSocket(skt);
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
SSLSocket skt = (SSLSocket) factory.createSocket(host, port);
skt.setEnabledProtocols(new String [] {defaultProtocol});
return customizeSSLSocket(skt);
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost,
int localPort) throws IOException, UnknownHostException {
SSLSocket skt = (SSLSocket) factory.createSocket(host, port,localHost, localPort);
skt.setEnabledProtocols(new String [] {defaultProtocol});
return customizeSSLSocket(skt);
}
@Override
public Socket createSocket(InetAddress address, int port,
InetAddress localAddress, int localPort) throws IOException {
SSLSocket skt = (SSLSocket) factory.createSocket(address, port, localAddress, localPort);
skt.setEnabledProtocols(new String [] {defaultProtocol});
return customizeSSLSocket(skt);
}
private Socket customizeSSLSocket(Socket skt) throws SocketException {
((SSLSocket) skt).addHandshakeCompletedListener(
new HandshakeCompletedListener() {
public void handshakeCompleted(
HandshakeCompletedEvent event) {
System.out.println("Handshake finished!");
Log.d("SSL ","Handshake finished!");
System.out.println(
"\t CipherSuite:" + event.getCipherSuite());
Log.d("CipherSuite ","Handshake finished!");
System.out.println(
"\t SessionId " + event.getSession());
Log.d("SessionId ","Handshake finished!");
System.out.println(
"\t PeerHost " + event.getSession().getPeerHost());
Log.d(" PeerHost ","Handshake finished!");
System.out.println(
"\t PeerHost " + event.getSession().getProtocol());
Log.d(" PeerHost ","Handshake finished!");
}
}
);
return skt;
}
}
问题是它不起作用!我已经与调试器进行了检查,一直以来,它始终具有所有受支持的协议(SSLv3,TLSV1,TLSv1.1 TLV1.2),如下图所示:
您是否知道为什么它继续不起作用?