首页 > 解决方案 > 如果先初始化外观,则不调用 SOCKS 代理身份验证器

问题描述

当使用需要身份验证的系统代理并使用-Djava.net.useSystemProxies=true选项调用 java 时,以下代码从不调用身份验证器:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        System.out.println("getPasswordAuthentication() called for " + getRequestingProtocol() + " proxy " + getRequestingHost() + ":" + getRequestingPort());
        return super.getPasswordAuthentication();
    }
});

new URL("https://www.google.com/").getContent();

如果外观未初始化,则调用身份验证器。这只发生在 SOCKS 代理上,始终执行 HTTP 代理的身份验证。

这是一个错误还是我做错了什么?

标签: javaproxysocks

解决方案


推荐阅读