Skip to content

Latest commit

 

History

History
29 lines (27 loc) · 953 Bytes

README.md

File metadata and controls

29 lines (27 loc) · 953 Bytes

Kotlin-URL-Expander

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
    }
}