简体   繁体   中英

java.net.Authenticator : java.net.ProtocolException: Server redirected too many times (20)

We are calling URL using proxy settings via java standalone sample code on our weblogic servers (node1/node2). This code works fine on node 1 but same code doesn't work on node2 server. We already checked proxy settings and credentials all are fine but still we get following error :

 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)

Also , Please find my code snippet below : SimpleAuthenticator.java

import java.net.Authenticator; import 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());
        }
}

Main class :

    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();
    }

We are stuck in this since months now and not able to get any useful information anywhere. kindly help.thanks

You will get this error if you provide wrong credentials (username or password). This happened to me with a Glassfish based web app. I was expecting a 401 response however.

I think, the Authenticator tries the same credentials many times.

Found this open issue, still unfixed it would seem. Your user doesn't have access rights, but instead of prompting again it retries with the same user over and over

Update: The issue had another ticket open - this is apparently expected behaviour and

"To overcome this, your implementation of Authenticator::getPasswordAuthentication needs to provide a means to collect the correct password if the initial authentication attempt was not successful."

See the ticket or the java.net.Authenticator documentation for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM