Skip to content

Instantly share code, notes, and snippets.

@icedraco
Created December 31, 2019 16:53
Show Gist options
  • Save icedraco/35ba24aa2c7fb8b21883a0bad6ef5021 to your computer and use it in GitHub Desktop.
Save icedraco/35ba24aa2c7fb8b21883a0bad6ef5021 to your computer and use it in GitHub Desktop.
HTTP requests using Kotlin and a built-in java.net.URL class
package org.icerealm.examples
import java.net.HttpURLConnection
import java.net.URL
fun main() {
val url = URL("https://postman-echo.com/status/403")
val con: HttpURLConnection = url.openConnection() as HttpURLConnection
con.requestMethod = "GET"
con.connectTimeout = 10*1000
con.readTimeout = 30*1000
con.doOutput = true
con.addRequestProperty("User-Agent", "Rawr/1.0")
val status = con.responseCode
val statusMsg = con.responseMessage
val headers = con.headerFields
val contentType = con.contentType
val body = String(con.inputStream.readAllBytes())
print("DONE") // set a breakpoint here to study this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment