Connecting to an HTTPS proxy using OkHttp
Asked Answered
T

1

6

Our environment just allows HTTPS connections so they provide us an HTTPS proxy which is the only way of connecting to the outside world. We have to route all traffics through this proxy as well as our Java application (a which uses OkHttp as its HTTP client.

We've setup the proxy like this:

Authenticator proxyAuthenticator = new Authenticator() {
    @Override
    public Request authenticate(Route route, Response response) throws IOException {
        String credential = Credentials.basic(username, password);
        return response.request().newBuilder()
                .header("Proxy-Authorization", credential)
                .build();
    }
};

return builder
        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
        .proxyAuthenticator(proxyAuthenticator)
        .build();

And we tested it using squid (as HTTP proxy). But we found out that this doesn't work in the real environment. as the proxy is HTTPS (SSL or TLS tunnelling?) not HTTP.

We could force all other Linux applications like wget to use the https proxy by setting the following environment variable:

export https_proxy='http://domain\user:password@prox-server:port'

But the Java application doesn't work and can't route traffics using this proxy.

BTW we couldn't config JVM to route all traffics through proxy by setting system properties http.proxyHost, http.proxyPort, https.proxyHost, https.proxyPort etc.

Is there any simple and straightforward way to set HTTPS proxy for a Java application?

Treponema answered 1/9, 2016 at 9:56 Comment(2)
Maybe this is for you: #121297Hay
@Hay as I said it didn't work.Treponema
E
1

For HTTPS you need to use https.proxyHost and https.proxyPort

https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html

Epiphytotic answered 1/9, 2016 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.