如何在Java的本机HTTP客户端上使用代理密码验证

问题描述 投票:1回答:1

如果代理对Java的本地HTTP客户端请求要求使用密码身份验证,我将尝试使用密码身份验证。

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

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

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

但是在发送请求时,我仍然收到错误407(在IP身份验证的代理上正常工作)。在通过Chrome扩展程序运行代理的浏览器上,代理可以正常运行。

java java-http-client
1个回答
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 );
© www.soinside.com 2019 - 2024. All rights reserved.