【问题标题】:https proxy using okhttp3使用 okhttp3 的 https 代理
【发布时间】:2021-11-22 16:52:36
【问题描述】:

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

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

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

【问题讨论】:

    标签: java okhttp


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢.. 它对我有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2015-02-05
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多