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

HttpURLConnection.setInstanceFollowRedirects has no apparent effect

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 6u11
    • core-libs
    • x86
    • linux

      JDK 5, 6, 7.

      ---%<---
      import java.io.IOException;
      import java.io.InputStream;
      import java.net.HttpURLConnection;
      import java.net.URL;
      public class FollowRedirects {
          public static void main(String[] args) throws Exception {
              HttpURLConnection.setFollowRedirects(true);
              URL u = new URL("http://grid.sonatype.org/ci/job/XWiki-Enterprise/api/xml");
              HttpURLConnection conn = (HttpURLConnection) u.openConnection();
              conn.setInstanceFollowRedirects(true);
              /*
              while (conn.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
                  u = new URL(conn.getHeaderField("Location"));
                  System.err.println("=> " + u);
                  conn = (HttpURLConnection) u.openConnection();
              }
               */
              System.err.println("code: " + conn.getResponseCode());
              dump(conn.getInputStream());
          }
          private static void dump(InputStream is) throws IOException {
              int c;
              while ((c = is.read()) != -1) {
                  System.err.write(c);
              }
          }
      }
      ---%<---

      results in

      ---%<---
      code: 302
      <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
      <html><head>
      <title>302 Found</title>
      </head><body>
      <h1>Found</h1>
      <p>The document has moved <a href="https://grid.sonatype.org/ci/job/XWiki-Enterprise/api/xml">here</a>.</p>
      <hr>
      <address>Apache/2.2.8 (Ubuntu) proxy_html/3.0.0 mod_ssl/2.2.8 OpenSSL/0.9.8g Server at grid.sonatype.org Port 80</address>
      </body></html>
      ---%<---

      If you comment out the while loop then it works, showing 200 status and the intended XML document.

      I know following redirects is documented to be the default, just pointing out that neither HttpURLConnection.setFollowRedirects(true) nor conn.setInstanceFollowRedirects(true) seem to make a difference.

            Unassigned Unassigned
            jglick Jesse Glick (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: