【发布时间】:2021-11-22 16:52:36
【问题描述】:
我正在使用 okhttp3 并尝试查看如何通过 userId 和 pswd 向仅接受 HTTPS 协议的代理服务器进行身份验证。我已经在其他网站(下面的链接)上看到过 SO 和示例,但他们没有 HTTPS 协议。
https://botproxy.net/docs/how-to/okhttpclient-proxy-authentication-how-to/
谁能告诉我如何使用HTTPS协议调用代理服务器?
【问题讨论】:
我正在使用 okhttp3 并尝试查看如何通过 userId 和 pswd 向仅接受 HTTPS 协议的代理服务器进行身份验证。我已经在其他网站(下面的链接)上看到过 SO 和示例,但他们没有 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();
【讨论】: