如果代理对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扩展程序运行代理的浏览器上,代理可以正常运行。
如果您尝试验证代理身份,通常需要在请求中添加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 );