如何在 ktor-client 中禁用重定向

How to disable redirections in ktor-client

我正在使用 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: Max send count 20 exceeded

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

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

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