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

We call the URL using the proxy settings using the separate java example code on our weblogic servers (node1 / node2). This code works fine on node 1, but the same code does not work on node2 server. We already checked the proxy settings and credentials, everything is in order, but still we get the 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 after a few months and cannot get any useful information anywhere.

+4
source share
2

, ( ). - Glassfish. 401.

, Authenticator .

+1

, , . ,

: - , ,

" , Authenticator::getPasswordAuthentication , . "

. java.net.Authenticator .

0

Source: https://habr.com/ru/post/1568102/


All Articles