使用 okhttp3 的 https 代理

https proxy using okhttp3

我正在使用 okhttp3 并尝试查看如何传递 userId 和 pswd 以向仅接受 HTTPS 协议的代理服务器进行身份验证。我已经在 SO 和其他网站(下面的link)上看到了 exmaple,但它们没有 HTTPS 协议。

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

谁能告诉我如何使用HTTPS协议调用代理服务器?

它不受官方支持,但有一个解决方法。

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