Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java代理类时支持身份验证_Java_Proxy - Fatal编程技术网

使用Java代理类时支持身份验证

使用Java代理类时支持身份验证,java,proxy,Java,Proxy,我正在使用java代理类并将其传递给HttpURLConnection.openConnection() 有没有办法向代理类提供身份验证信息(就像http.proxyUser和http.proxyPassword一样? 谢谢您可以使用: Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthenticatio

我正在使用java代理类并将其传递给HttpURLConnection.openConnection() 有没有办法向代理类提供身份验证信息(就像http.proxyUser和http.proxyPassword一样?
谢谢

您可以使用:

Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("login", "password".toCharArray());
        }
});

它会影响整个通信还是只影响我正在打开的相关连接?@danieln Authenticator是静态的,因此它对您的应用程序是全局的。但只有在必要时才会调用它。如果您打开了其他不需要身份验证的连接,它将不会被调用。因此,它对我的应用程序是全局的,但对整个应用程序服务器不是全局的?(如http.proxyUser)。如果我想对不同的连接进行不同的身份验证,该怎么办?@danieln在方法
getPasswordAuthentication()
中,您可以询问身份验证程序,以了解请求的身份验证是针对哪个请求的(
getRequestingURL()
等…)