Skip to content
Advertisement

https proxy using okhttp3

I am using okhttp3 and trying to see how do I pass userId & pswd to authenticate with proxy server that accepts only HTTPS protocol. I already saw exmaple over SO & on other sites(link below) but they don’t have HTTPS protocol.

https://botproxy.net/docs/how-to/okhttpclient-proxy-authentication-how-to/

Can anyone please tell me how to use HTTPS protocol to call proxy server?

Advertisement

Answer

It is not officially supported but there is a workaround.

https://github.com/square/okhttp/issues/6561

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

OkHttpClient client = new OkHttpClient.Builder()
        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
        .proxyAuthenticator(proxyAuthenticator);
        .socketFactory(new DelegatingSocketFactory(SSLSocketFactory.getDefault()))
        .build();

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement