My program needs to login to a certain website, and I'm using Authenticator to do this. If I supply valid credentials this works fine. However, my program needs to handle the case of the user entering invalid credentials. The site responds to invalid logins by redirecting to itself. If I type the URL into a browser, I get a login prompt, and if I enter an invalid login, it just reloads the page and gives me a new login prompt.
Since java automatically tries to login to every page with the given authenticator, it gets stuck in a loop. Login, redirected to same site, login again, etc. until this exception is thrown:
java.net.ProtocolException: Server redirected too many times (20)
What's the best way to handle this? I've tried adding
HttpURLConnection.setFollowRedirects(false);
but it doesn't seem to work (it still redirects).