title: java.net.Authenticator : java.net.ProtocolException: Server redirected too many times (20)
body: <p>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 : </p>
  
  <pre><code> 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.&lt;init&gt;(Reader.java:61)
          at java.io.InputStreamReader.&lt;init&gt;(InputStreamReader.java:55)
          at ProxyCode.readFromInputStream(ProxyCode.java:65)
          at ProxyCode.start2(ProxyCode.java:59)
          at ProxyCode.main(ProxyCode.java:22)
  </code></pre>
  
  <p>Also , Please find my code snippet below :
  SimpleAuthenticator.java</p>
  
  <p>import java.net.Authenticator;
  import java.net.PasswordAuthentication;</p>
  
  <pre><code>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());
          }
  }
  </code></pre>
  
  <p>Main class :</p>
  
  <pre><code>    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();
      }
  </code></pre>
  
  <p>We are stuck in this since months now and not able to get any useful information anywhere.
  kindly help.thanks</p>