Skip to content

Instantly share code, notes, and snippets.

@bchapuis
Created May 14, 2012 07:24
Show Gist options
  • Select an option

  • Save bchapuis/2692450 to your computer and use it in GitHub Desktop.

Select an option

Save bchapuis/2692450 to your computer and use it in GitHub Desktop.
Compare two urls and follow redirects
public static boolean isSame(URL a, URL b, int depth) throws IOException {
HttpURLConnection conn = (HttpURLConnection) b.openConnection();
conn.setInstanceFollowRedirects(false);
conn.connect();
int d = 0;
while (conn.getResponseCode() >= 300 && conn.getResponseCode() <= 399 && d < depth) {
b = new URL(conn.getHeaderField("Location"));
conn = (HttpURLConnection) b.openConnection();
conn.setInstanceFollowRedirects(false);
conn.connect();
depth++;
}
return a.equals(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment