【问题标题】:proxy host name,port,username,password with httpurlconnection代理主机名、端口、用户名、带有 httpurl 连接的密码
【发布时间】:2019-07-25 22:32:42
【问题描述】:
private static void setProxy(String proxyHostName,int proxyport){
    proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyHostName,proxyport));
}

private static void setProxy(String proxyHostName,int proxyport,String username,String password){
    setProxy(proxyHostName,proxyport);
    if (username!=null && password!=null) {
        Authenticator authenticator = new Authenticator() {

            public PasswordAuthentication getPasswordAuthentication() {
                return (new PasswordAuthentication(username, password.toCharArray()));
            }
        };
        Authenticator.setDefault(authenticator);
    }

}

这是代理设置的代码。我不知道为什么会抛出这个错误。

例外:

java.io.IOException:无法通过代理隧道。代理返回 ""HTTP/1.1 407 需要代理身份验证"" 在 sun.net.http://www.protocol.http.httpurlconnection.dotunneling%28httpurlconnection.java:2142/) 在 sun.net.http://www.protocol.https.abstractdelegatehttpsurlconnection.connect%28abstractdelegatehttpsurlconnection.java:183/) 在 sun.net.http://www.protocol.https.httpsurlconnectionimpl.connect%28httpsurlconnectionimpl.java:162/) ..

【问题讨论】:

    标签: java


    【解决方案1】:

    您的代码不完整,并且您没有指定您使用的 Java 版本,所以我不能肯定地说,但我猜这可能是原因: unable to tunnel through proxy since java-8-update-111.

    尝试像这样修改您的 setProxy() 方法:

    private static void setProxy(String proxyHostName, int proxyport, String username, String password){
        setProxy(proxyHostName,proxyport);
        if (username!=null && password!=null) {
            System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
            Authenticator authenticator = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return (new PasswordAuthentication(username, password.toCharArray()));
                }
            };
            Authenticator.setDefault(authenticator);
        }
    }
    

    或者使用-Djdk.http.auth.tunneling.disabledSchemes= 运行您的应用程序。

    有关 Java 中经过身份验证的代理的更多详细信息,可以在此处找到:authenticated http proxy with java

    这里也有对此行为的解释:JDK-8210814

    【讨论】:

    • jdk版本->1.8.0_91
    • 你试过我的解决方案了吗?无法保证 Java 1.8.0_111 中添加了此限制。他们以前可能根本没有注意到。
    • 是的,我已经尝试过您的解决方案:@Konard Botor。成功了。谢谢
    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多