【问题标题】:Redirect to absolute URL in Ktor重定向到 Ktor 中的绝对 URL
【发布时间】:2019-12-18 00:26:22
【问题描述】:

我正在学习如何使用 ktor。对于特殊用例,我需要从我的 ktor 服务器重定向到不同的域。但是我现在不能让它正常工作。

作为一个简单的例子,我有一个 Application.kt

import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

fun main(args: Array<String>) {
    val server = embeddedServer(Netty, port = 8080) {
        routing {
         get("/") {
           call.respondRedirect("www.google.com")
        }}
    }
}
server.start(wait = true)

}

除了它将我重定向到www.google.com之外,它会将我重定向到localhost:8080/www.google.com

【问题讨论】:

    标签: kotlin ktor


    【解决方案1】:

    想通了。您还需要设置协议。这行得通

    call.respondRedirect("https://google.com/")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2015-06-12
      • 2023-03-09
      • 2021-04-05
      • 1970-01-01
      • 2019-03-28
      相关资源
      最近更新 更多