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.
---%<---
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.
- relates to
-
JDK-4620571 urlconnection following redirect uses protocol of original request
-
- Resolved
-