Created
December 31, 2019 16:53
-
-
Save icedraco/35ba24aa2c7fb8b21883a0bad6ef5021 to your computer and use it in GitHub Desktop.
HTTP requests using Kotlin and a built-in java.net.URL class
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
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