2016-12-02 59 views
0
String currentUrl = "https://www.test.com/"; 
conn = (HttpsURLConnection) (currentUrl.openConnection()) 

如果我使用域,一切正常工作。HttpsURLConnection與IP問題

不過,如果我使用域的IP地址,發生

String currentUrl = "https://123.123.123.123:443/"; 
conn = (HttpsURLConnection) (currentUrl.openConnection()); 

問題,我得到了SSLPeerUnverifiedException(Hostname 123.123.123.123 not verified異常,並請求失敗。

爲了解決這個問題,我只是提供的HostnameVerifier

conn.setHostnameVerifier(new HostnameVerifier() { 

    @Override 
    public boolean verify(String hostname, SSLSession session) { 
     String host = "www.test.com"; 

     return HttpsURLConnection.getDefaultHostnameVerifier().verify(host, session); 
    } 
}); 
除了

,我要解決的SNI情況相同的問題。

,所以我提供一個自定義的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; 
    } 
} 

不幸的是,它還有另外一個問題, 不能重用連接更多,這意味着每個請求必須TCP握手和SSL握手,它會花費很多時間。

所以這裏是我的問題, 是否有解決HttpsUrlConnection的直接IP請求問題?

如果沒有,我如何重新使用連接根據我上面提到的。

回答

0

試試這個:

conn.setHostnameVerifier(new HostnameVerifier() { 

    @Override 
    public boolean verify(String hostname, SSLSession session) { 
     return true; 
    } 

});