String currentUrl = "https://www.test.com/";
conn = (HttpsURLConnection) (currentUrl.openConnection())
Wenn ich Domain verwende, funktioniert alles perfekt.HttpsURLConnection mit IP Problem
Allerdings, wenn ich die IP-Adresse der Domain verwenden,
String currentUrl = "https://123.123.123.123:443/";
conn = (HttpsURLConnection) (currentUrl.openConnection());
Problem tritt auf, ich habe die SSLPeerUnverifiedException(Hostname 123.123.123.123 not verified
Ausnahme, und die Anforderung ist fehlgeschlagen.
Um dieses Problem zu beheben, ich biete nur einem HostnameVerifier
conn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
String host = "www.test.com";
return HttpsURLConnection.getDefaultHostnameVerifier().verify(host, session);
}
});
zusätzlich, ich habe das gleiche Problem der SNI Situation zu lösen.
so bieten ich eine benutzerdefinierte SSLSocketFactory
MySSLSocketFactory sslSocketFactory = new MySSLSocketFactory(conn);
conn.setSSLSocketFactory(sslSocketFactory);
public class MySSLSocketFactory extends SSLSocketFactory{
private HttpsURLConnection conn;
public HalleySSLSocketFactory(HttpsURLConnection conn) {
this.conn = conn;
}
@Override
public Socket createSocket() throws IOException {
return null;
}
@Override
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
return null;
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost,
int localPort) throws IOException, UnknownHostException {
return null;
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return null;
}
@Override
public Socket createSocket(InetAddress address, int port,
InetAddress localAddress, int localPort) throws IOException {
return null;
}
@Override
public String[] getDefaultCipherSuites() {
return new String[0];
}
@Override
public String[] getSupportedCipherSuites() {
return new String[0];
}
@Override
public Socket createSocket(Socket plainSocket, String host, int port,
boolean autoClose) throws IOException {
String peerHost = this.conn.getRequestProperty("Host");
if (peerHost == null)
peerHost = host;
InetAddress address = plainSocket.getInetAddress();
if (autoClose) {
plainSocket.close();
}
SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory
.getDefault(0);
SSLSocket ssl = (SSLSocket) sslSocketFactory
.createSocket(address, port);
ssl.setEnabledProtocols(ssl.getSupportedProtocols());
// set up SNI before the handshake
try {
java.lang.reflect.Method setHostnameMethod = ssl.getClass()
.getMethod("setHostname", String.class);
setHostnameMethod.invoke(ssl, peerHost);
} catch (Exception e) {
FileLog.w(TAG, "SNI not useable", e);
}
return ssl;
}
}
Leider hat es noch ein weiteres Problem, es die Verbindung nicht mehr wiederverwenden können, die jede Anforderung müssen TCP-Handshake und SSL-Handshake bedeutet und es wird kostet viel Zeit.
Also hier ist meine Frage, gibt es sowieso um das direkte IP-Anfrage-Problem mit HttpsUrlConnection zu lösen?
wenn nein, wie kann ich die Verbindung wiederverwenden, wie oben erwähnt.