2

如果代理需要 Java 的本机 HTTP 客户端请求,我正在尝试使用密码身份验证。

我尝试了很多东西,适用于我的程序的最常见的解决方案是

String username = "username";
String password = "password";

Authenticator.setDefault( new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password.toCharArray());
            }
        });

但在那之后,我在发送请求时仍然收到错误 407(在 IP Authenticated 代理上工作正常)。该代理在通过 Chrome 扩展程序运行它的浏览器上运行良好。

4

2 回答 2

0

如果您尝试对代理进行身份验证,通常需要Proxy-Authorization在请求中添加标头。

请参阅:https ://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization

就像是

String authentication = "username:password";
String authenticationEncoded = Base64.getEncoder().encodeToString(authentication .getBytes());
HttpHeaders headers = new HttpHeaders();
headers.add("Proxy-Authorization", "Basic " + authenticationEncoded );
于 2019-12-25T21:06:34.130 回答
0

Like the HttpURLConnection the java.net.http.HttpClient honors some system properties that allow to control (enable/disable) some proxy authentication schemes. If your proxy requires authentication you may have to tune the default values of these properties (jdk.http.auth.tunneling.disabledSchemes and jdk.http.auth.proxying.disabledSchemes).
See:
core-libs/java.net
Disable Basic authentication for HTTPS tunneling
From https://www.oracle.com/technetwork/java/javase/8all-relnotes-2226344.html

于 2020-01-07T11:17:40.150 回答