Send GET Request using Kotlin

HttpURLConnection class

package app

import java.net.HttpURLConnection
import java.net.URL

fun main()
{
    val url = URL("https://httpbin.org/get")

    val builder = StringBuilder()

    with (url.openConnection() as HttpURLConnection) {
        requestMethod = "GET"

        inputStream.bufferedReader().use {
            var line = it.readLine()
            while (line != null) {
                builder.appendln(line)
                line = it.readLine()
            }
        }
    }

    val content = builder.toString()

    println(content)
}

Leave a Comment

Cancel reply

Your email address will not be published.