Kotlin中的HTTP请求 [英] HTTP Request in Kotlin
问题描述
我对Kotlin完全陌生.我想使用POST方法进行登录验证,并使用GET方法获取一些信息.我已经有上一个项目的URL,服务器用户名和密码.我没有找到任何适当的示例项目使用此东西.任何人都可以向我建议任何可以在HTTP请求中使用GET和POST方法的工作示例
I'm completely new to Kotlin. I want to do a login validation using POST method and to get some information using GET method. I've URL, server Username and Password already of my previous project. I didn't find any proper example project which uses this thing. Anyone please suggest me any working example where I can use GET and POST method in HTTP request
推荐答案
对于Android, Volley 是入门的好地方.对于所有平台,您可能还想签出 ktor 客户端.
For Android, Volley is a good place to get started. For all platforms, you might also want to check out ktor client.
但是,您可以使用Java中使用的标准库.例如,使用HttpURLConnection
,您可以执行以下操作:
However, you can use standard libraries you would use in Java. For example, with HttpURLConnection
you can do:
fun sendGet() {
val url = URL("http://www.google.com/")
with(url.openConnection() as HttpURLConnection) {
requestMethod = "GET" // optional default is GET
println("\nSent 'GET' request to URL : $url; Response Code : $responseCode")
inputStream.bufferedReader().use {
it.lines().forEach { line ->
println(line)
}
}
}
}
或更简单:
URL("https://google.com").readText()
这篇关于Kotlin中的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!