United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6810084 : HttpURLConnection.setInstanceFollowRedirects has no apparent effect

Details
Type:
Bug
Submit Date:
2009-02-25
Status:
Closed
Updated Date:
2010-09-14
Project Name:
JDK
Resolved Date:
2010-09-14
Component:
core-libs
OS:
linux
Sub-Component:
java.net
CPU:
x86
Priority:
P3
Resolution:
Not an Issue
Affected Versions:
6u11
Fixed Versions:

Related Reports
Relates:

Sub Tasks

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.
                                     
2010-09-14
PUBLIC COMMENTS

Seems to be root cause of

http://www.netbeans.org/nonav/issues/show_bug.cgi?id=159115
                                     
2009-02-25



Hardware and Software, Engineered to Work Together