Created
October 16, 2012 14:50
-
-
Save sbglasius/3899754 to your computer and use it in GitHub Desktop.
Groovy function that will follow a number of redirects to get the real URL of a webpage/service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findRealUrl(url) { | |
HttpURLConnection conn = url.openConnection() | |
conn.followRedirects = false | |
conn.requestMethod = 'HEAD' | |
if(conn.responseCode in [301,302]) { | |
if (conn.headerFields.'Location') { | |
return findRealUrl(conn.headerFields.Location.first().toURL()) | |
} else { | |
throw new RuntimeException('Failed to follow redirect') | |
} | |
} | |
return url | |
} |
mahmoudimus
commented
Dec 7, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment