Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4637447

HttpURLConnection's setFollowRedirects(false) doesnt work for the instance

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • None
    • sparc
    • solaris_8

      HttpURLConnection's setFollowRedirects(false) doesnt work for the instance on which its been called. Even though we set this to false , it does redirects automatically.

      How to reproduce it:
      ====================
      Take the following test client and servlet

      import java.net.*;
      import java.io.*;

      public class RedirectTestClient {


              public static void main(String[] args) {

                      HttpURLConnection ucon = null;
                      int responseCode;

                      try {
                                      URL url = new URL("http://javacup.sfbay.sun.com:8080/examples/servlet/HttpRedirect");
                                      
                                      System.out.println("Before opening url connection ");
                                      ucon = (HttpURLConnection) url.openConnection();
                                      ucon.setFollowRedirects(false);
          
                                      System.out.println("Before connect ");
                                      ucon.connect();
                                      System.out.println("After connect ");
                                      System.out.println(" Get follow redirects " + ucon.getFollowRedirects());
                                      

                                      System.out.println(" Before getting response code ");
                                      try {
                                              System.out.println(" Response code " + ucon.getResponseCode());
                                      } catch(ProtocolException pexp) { pexp.printStackTrace(); }

                                      System.out.println(" After getting response code ");
                                      ucon.disconnect();
                                      System.out.println("After disconnect ");

                                      ucon = (HttpURLConnection) url.openConnection();
                                      System.out.println("Before connect ");
                                      ucon.connect();
                                      System.out.println("After connect ");
                                      System.out.println(" Get follow redirects " + ucon.getFollowRedirects());
                                      System.out.println(" Before getting response code ");
                                      System.out.println(" Response code " + ucon.getResponseCode());
                                      System.out.println(" After getting response code ");

                      }catch(Exception exp) {
                              exp.printStackTrace();
                      }

              } // End of method

      } // End of class

      Servlet class:
      ---------------

      import java.io.*;
      import javax.servlet.*;
      import javax.servlet.http.*;

      public class HttpRedirect extends HttpServlet {

          public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException
          {
              try {
                      System.out.println("In HttpRedirect servlets do get");

                      response.setContentType("text/html");
                      response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);

                      System.out.println("In HttpRedirect servlets before redirect ");
                      response.sendRedirect("http://javacup.sfbay.sun.com:8080/examples/servlet/HttpRedirect");
                      

              } catch(Exception exp) {
                      exp.printStackTrace();
              }

         } // End of doGet
      }

      Output of the above client execution with merlin build 92 on solaris 8 with tomcat server:

      java RedirectTestClient
      Before opening url connection
      Before connect
      After connect
       Get follow redirects false
       Before getting response code
      java.net.ProtocolException: Server redirected too many times (20)
              at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:700)
              at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:1144)
              at sun.net.www.protocol.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:1158)
              at RedirectTestClient.main(RedirectTestClient.java:31)
       After getting response code
      After disconnect
      Before connect
      After connect
       Get follow redirects false
       Before getting response code
       Response code 302
       After getting response code




            alanb Alan Bateman
            vakellasunw Venkata Akella (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: