4

I'm trying to access https webpages using proxy according to How do a send an HTTPS request through a proxy in Java?

But I ran into a strange problem: the HttpsURLConnection is ignoring the setSSLSocketFactory method. Here are my codes:

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

This is the SSLTunnelSocketFactory class:

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();
    }
}

And here is the output:

calling SSLTunnelSocketFactory
Exception in thread "main" java.lang.RuntimeException: java.net.ConnectException: Connection timed out: connect

You can see that "calling createSocket" was not printed, and a timeout exception occured. This means that the setSSLSocketFactory method is ignored, which should not happen.

Could you please help me deal with this problem?

Community
  • 1
  • 1
user3254156
  • 133
  • 1
  • 7

1 Answers1

0

Seems like you need to set connection.setDoInput(true); connection.setDoOutput(true);

Refer to this solution-https://community.oracle.com/thread/1536034?start=0&tstart=0

user2254601
  • 164
  • 2
  • 8