JDK-6810084 : HttpURLConnection.setInstanceFollowRedirects has no apparent effect
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6u11
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2009-02-25
  • Updated: 2010-09-14
  • Resolved: 2010-09-14
Related Reports
Relates :  
Description
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.

Comments
EVALUATION The protocol of the requested URL is 'http', and the protocol of the redirected URL is 'https'. A decision was made a long time ago not to support automatically follow redirect from one protocol to another, see the evaluation section of CR 4620571.
14-09-2010

PUBLIC COMMENTS Seems to be root cause of http://www.netbeans.org/nonav/issues/show_bug.cgi?id=159115
25-02-2009