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

时间:2011-10-26 12:53:13

标签: java ntlm java-7

我在类似于this one的网络应用程序中使用NTLM身份验证来从其他域中重新检索某些数据。过去工作得很好。

JDK 1.7升级后,我开始收到java.net.ProtocolException: Server redirected too many times (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());
    }
}

在这方面有谁可以帮助我?

0 个答案:

没有答案