HttpsURLConnection忽略设置SLsocketFactory

HttpsURLConnection忽略设置SLsocketFactory,https,proxy,Https,Proxy,我正在尝试根据使用代理访问https网页 但我遇到了一个奇怪的问题:HttpsURLConnection忽略了setssSocketFactory方法。 这是我的密码: HttpsURLConnection connection = (HttpsURLConnection) new URL("https://www.google.com").openConnection(); connection.setSSLSocketFactory(new SSLTunnelSocketFactory("1

我正在尝试根据使用代理访问https网页

但我遇到了一个奇怪的问题:HttpsURLConnection忽略了setssSocketFactory方法。 这是我的密码:

HttpsURLConnection connection = (HttpsURLConnection) new URL("https://www.google.com").openConnection();
connection.setSSLSocketFactory(new SSLTunnelSocketFactory("127.0.0.1", 8089));
这是SSLTunnelSocketFactory类:

class SSLTunnelSocketFactory extends SSLSocketFactory {
    public SSLTunnelSocketFactory(String proxyIpAddress, int proxyPort) {
        System.out.println("calling SSLTunnelSocketFactory");
    }
    @Override
    public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
        System.out.println("calling createSocket");
    }

    @Override
    public Socket createSocket(String s, int i) throws IOException,
            UnknownHostException {
        throw new NotImplementedException();
    }

    @Override
    public Socket createSocket(String s, int i, InetAddress inetAddress,
                               int i2) throws IOException,
            UnknownHostException {
        throw new NotImplementedException();
    }

    @Override
    public Socket createSocket(InetAddress inetAddress,
                               int i) throws IOException {
        throw new NotImplementedException();
    }

    @Override
    public Socket createSocket(InetAddress inetAddress, int i,
                               InetAddress inetAddress2,
                               int i2) throws IOException {
        throw new NotImplementedException();
    }

    @Override
    public String[] getDefaultCipherSuites() {
        throw new NotImplementedException();
    }

    @Override
    public String[] getSupportedCipherSuites() {
        throw new NotImplementedException();
    }
}
以下是输出:

calling SSLTunnelSocketFactory
Exception in thread "main" java.lang.RuntimeException: java.net.ConnectException: Connection timed out: connect
您可以看到“调用createSocket”未打印,并且发生超时异常。 这意味着setSSLSocketFactory方法被忽略,这不应该发生

你能帮我解决这个问题吗?

看来你需要设置 connection.setDoInput(true); connection.setDoOutput(真)


请参阅此解决方案-

问题是否已解决?如果是,请让我们知道解决方案