java.net.ProtocolException:服务器重定向次数过多 (20)

发布于 2024-12-12 06:51:29 字数 1628 浏览 0 评论 0原文

我在 Web 应用程序中使用 NTLM 身份验证,类似于 这个可以从另一个域中平静地检索一些数据。它曾经工作得很好。

JDK 1.7 升级后,我开始收到 java.net.ProtocolException:服务器重定向次数过多 (20) 异常。我不知道这是否是由于 1.7 升级而发生的(这可能只是巧合)。

代码与此几乎相同:

// Install the custom authenticator
Authenticator.setDefault(new MyAuthenticator());

// Access the page
try {
    // Create a URL for the desired page
    URL url = new URL("http://hostname:80/index.html");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

public class MyAuthenticator extends Authenticator {
    // This method is called when a password-protected URL is accessed
    protected PasswordAuthentication getPasswordAuthentication() {
        // Get information about the request
        String promptString = getRequestingPrompt();
        String hostname = getRequestingHost();
        InetAddress ipaddr = getRequestingSite();
        int port = getRequestingPort();

        // Get the username from the user...
        String username = "myusername";

        // Get the password from the user...
        String password = "mypassword";

        // Return the information
        return new PasswordAuthentication(username, password.toCharArray());
    }
}

有人可以在这方面帮助我吗?

I'm using NTLM authentication in my web application similar to this one to restfully retrieve some data from another domain. It used to work fine.

After JDK 1.7 upgrade, I've started receiving java.net.ProtocolException: Server redirected too many times (20) exception. I don't know if it's happening due to 1.7 upgrade (it could just be a coincidence).

The code is pretty much the same as this:

// Install the custom authenticator
Authenticator.setDefault(new MyAuthenticator());

// Access the page
try {
    // Create a URL for the desired page
    URL url = new URL("http://hostname:80/index.html");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

public class MyAuthenticator extends Authenticator {
    // This method is called when a password-protected URL is accessed
    protected PasswordAuthentication getPasswordAuthentication() {
        // Get information about the request
        String promptString = getRequestingPrompt();
        String hostname = getRequestingHost();
        InetAddress ipaddr = getRequestingSite();
        int port = getRequestingPort();

        // Get the username from the user...
        String username = "myusername";

        // Get the password from the user...
        String password = "mypassword";

        // Return the information
        return new PasswordAuthentication(username, password.toCharArray());
    }
}

Could anyone please help me in this regard?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文