【问题标题】:java.net.Authenticator : java.net.ProtocolException: Server redirected too many times (20)java.net.Authenticator : java.net.ProtocolException: 服务器重定向太多次 (20)
【发布时间】:2015-02-16 22:17:18
【问题描述】:

我们通过 weblogic 服务器 (node1/node2) 上的 java 独立示例代码使用代理设置调用 URL。 此代码在节点 1 上运行良好,但相同的代码在节点 2 服务器上不起作用。 我们已经检查了代理设置和凭据都很好,但我们仍然收到以下错误:

 java.net.ProtocolException: Server redirected too many  times (20)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1323)
        at ProxyCode.start2(ProxyCode.java:54)
        at ProxyCode.main(ProxyCode.java:23)
Exception in thread "Main Thread" java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:61)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
        at ProxyCode.readFromInputStream(ProxyCode.java:65)
        at ProxyCode.start2(ProxyCode.java:59)
        at ProxyCode.main(ProxyCode.java:22)

另外,请在下面找到我的代码 sn-p: SimpleAuthenticator.java

导入 java.net.Authenticator; 导入 java.net.PasswordAuthentication;

public class SimpleAuthenticator extends Authenticator
{
        private String username;
        private String password;

        public SimpleAuthenticator(String username,String password)
        {
                this.username = username;
                this.password = password;
        }

        protected PasswordAuthentication getPasswordAuthentication()
        {
                return new PasswordAuthentication(
                        username,password.toCharArray());
        }
}

主类:

    String url = "http://www.oracle.com/technetwork/java/readme-2-149793.txt";
    String proxy = "proxyserver";
    String port = "8080";
    String username = "username";
    String password = "password";
    Authenticator.setDefault(new SimpleAuthenticator(username,password));

    URL server = null;
    try {

            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
            server = new URL(url);
    }
    catch (MalformedURLException e) {
            e.printStackTrace();
    }

Properties systemProperties = System.getProperties();
    systemProperties.setProperty("http.proxyHost", proxy);
    systemProperties.setProperty("http.proxyPort", port);

    InputStream in = null;
    URLConnection connection = null;

    try {
            connection = (URLConnection) server.openConnection();
            connection.connect();
            in = connection.getInputStream();
    }
    catch (IOException e) {
            e.printStackTrace();
    }
            System.out.println(readFromInputStream(in));
    }

    public static String readFromInputStream(InputStream in) {
            StringBuffer strBuf = new StringBuffer();
            char ac[];
            BufferedReader buf = new BufferedReader(new InputStreamReader(in));

      try 
     {
            while (buf.ready()) {
                    ac = new char[10000];
                    buf.read(ac);
                    strBuf.append(ac);
     }
            buf.close();
    }
    catch (IOException e) {
            e.printStackTrace();
    }

几个月以来,我们一直陷入困境,无法在任何地方获得任何有用的信息。 请帮忙。谢谢

【问题讨论】:

  • 您是否使用过 Firebug 或 chrome/IE 开发人员工具来查看进行了哪些重定向?有时这可以为正在发生的事情提供线索。
  • 我在 unix box 上运行它。使用 CURL 测试结果是肯定的。它可以工作..但是从 JAVA 代码调用 URL 不起作用。
  • 我仍然认为查看重定向的内容会有所帮助。 getfirebug.com/network
  • 请问还有其他选择吗?

标签: java http-proxy


【解决方案1】:

如果您提供错误的凭据(用户名或密码),您将收到此错误。 这发生在我的一个基于 Glassfish 的网络应用程序中。然而,我期待一个 401 响应。

我认为,Authenticator 会多次尝试相同的凭据。

【讨论】:

    【解决方案2】:

    发现了这个未解决的问题,似乎仍未解决。您的用户没有访问权限,但不会再次提示它retries with the same user over and over

    更新:该问题已打开另一张票 - 这显然是预期的行为,并且

    “为了克服这个问题,您的实施 Authenticator::getPasswordAuthentication 需要提供一种方法 如果初始身份验证尝试是,则收集正确的密码 不成功。”

    查看票证或the java.net.Authenticator documentation了解更多信息。

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 2018-11-22
      • 2012-06-16
      • 2023-03-25
      • 2017-04-19
      • 1970-01-01
      • 2017-04-12
      • 2020-07-02
      • 2013-01-01
      相关资源
      最近更新 更多