object URLExpander {
fun expand(url: String): String {
var connection: HttpURLConnection
var finalUrl = url
try {
do {
connection = URL(finalUrl).openConnection() as HttpURLConnection
connection.instanceFollowRedirects = false
connection.useCaches = false
connection.requestMethod = "GET"
connection.connect()
val responseCode = connection.responseCode
if (responseCode in 300..399) {
val redirectedUrl = connection.getHeaderField("Location") ?: break
finalUrl = redirectedUrl
} else break
} while (connection.responseCode != HttpURLConnection.HTTP_OK)
connection.disconnect()
} catch (e: Exception) {
e.printStackTrace()
}
return finalUrl
}
}
-
Notifications
You must be signed in to change notification settings - Fork 0
imamhossain94/kt-url-expander
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
Expand shorten URL by tracking redirect URL.
Topics
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published