【问题标题】:How to disable redirections in ktor-client如何在 ktor-client 中禁用重定向
【发布时间】:2019-10-12 01:27:23
【问题描述】:

我正在使用 ktor-client(ApacheHttpClient 引擎)创建简单的 HTTP 请求

val client = HttpClient(Apache) {
    engine {
        followRedirects = false
        this@HttpClient.expectSuccess = false
    }
}

并使用它来提交表单

client.submitForm<HttpResponse>(
        url = "https://foo.com/login",
        formParameters = Parameters.build {
            append("_username", username)
            append("_password", password)
        })

在日志中,我可以看到正确的 302 重定向响应,我想从中获取并获取 cookie。但是相反,我看到客户端继续前进并发出更多请求,最终失败:

io.ktor.client.features.SendCountExceedException:超过最大发送计数 20

如何在 ktor-client 中完全禁用基于 302 的重定向?

【问题讨论】:

    标签: kotlin httpclient ktor


    【解决方案1】:

    ktor-client follows redirects by default,防止无限重定向使用:

    val client = HttpClient(HttpClientEngine) {
        followRedirects = false
    }
    

    【讨论】:

    • 是的,你是对的。我错过了放置followRedirects = false 的正确位置。我已将其放入engine,但正确的位置更高。
    猜你喜欢
    • 2020-06-12
    • 2021-07-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2019-12-18
    • 2015-01-21
    • 1970-01-01
    相关资源
    最近更新 更多