Skip to content

Instantly share code, notes, and snippets.

@afkvido
Last active February 28, 2023 21:16
Show Gist options
  • Save afkvido/ff65086bc60e606ef6a00f86ef6316c6 to your computer and use it in GitHub Desktop.
Save afkvido/ff65086bc60e606ef6a00f86ef6316c6 to your computer and use it in GitHub Desktop.
Read data from URL in Kotlin.
import java.net.URI
import java.net.URL
import java.util.*
/** Get text from an online resource */
fun fetch (url : URL) : String {
println("Fetching resource from $url")
return Scanner(url.openStream(), "UTF-8").useDelimiter("\\A").next()
}
fun fetch (url : String) : String {
return fetch(URL(url))
}
fun fetch (url : URI) : String {
return fetch(url.toURL())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment