41 lines
1.6 KiB
Markdown
41 lines
1.6 KiB
Markdown
# Ktor based http client
|
|||
|
|
|
||
|
|
```kotlin
|
||
|
|
private val httpClient = HttpClient()
|
||
|
|
|
||
|
|
public val CustomProtocolContext: ProtocolContextBuilder.() -> Unit = {
|
||
|
|
authProvider = AuthenticationProvider { url, accessToken, uuid, serverIdHash ->
|
||
|
|
val status = httpClient.post(url) {
|
||
|
|
headers { header("Content-Type", "application/json") }
|
||
|
|
setBody("{\"accessToken\":\"$accessToken\", \"selectedProfile\":\"$uuid\", \"serverId\":\"$serverIdHash\"}")
|
||
|
|
}.status
|
||
|
|
require(status == HttpStatusCode.NoContent)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
# HttpURLConnection based http client
|
||
|
|
|
||
|
|
```kotlin
|
||
|
|
private fun sendJoinRequest(url: String, accessToken: String, uuid: String, serverIdHash: String): Boolean {
|
||
|
|
val connection = (URL(url).openConnection() as HttpURLConnection).apply {
|
||
|
|
requestMethod = "POST"
|
||
|
|
setRequestProperty("Content-Type", "application/json")
|
||
|
|
doOutput = true
|
||
|
|
connectTimeout = 5000
|
||
|
|
readTimeout = 5000
|
||
|
|
}
|
||
|
|
val jsonPayload = """{"accessToken":"$accessToken","selectedProfile":"$uuid","serverId":"$serverIdHash"}"""
|
||
|
|
connection.outputStream.use { os -> os.write(jsonPayload.toByteArray(Charsets.UTF_8)) }
|
||
|
|
val responseCode = connection.responseCode
|
||
|
|
connection.disconnect()
|
||
|
|
return responseCode == HttpURLConnection.HTTP_NO_CONTENT
|
||
|
|
}
|
||
|
|
|
||
|
|
public val CustomProtocolContext: ProtocolContextBuilder.() -> Unit = {
|
||
|
|
authProvider = AuthenticationProvider { url, accessToken, uuid, serverIdHash ->
|
||
|
|
val success = sendJoinRequest(url, accessToken, uuid, serverIdHash)
|
||
|
|
require(success) { "Failed to authenticate with Mojang session server" }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|