HttpsURLConnection与IP问题

时间:2016-12-02 07:17:24

标签: connection ip httpsurlconnection

String currentUrl = "https://www.test.com/";
conn = (HttpsURLConnection) (currentUrl.openConnection())

如果我使用域名,一切都很完美。

但是,如果我使用域名的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请求问题?

如果不是,我如何根据我上面提到的重用连接。

1 个答案:

答案 0 :(得分:1)

试试这个:

conn.setHostnameVerifier(new HostnameVerifier() {

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

});