英文:
Ktor client closes before response
问题
我正在尝试使用 Ktor 创建一个 HTTP 客户端。使用教程中的代码,我有以下代码块:
suspend fun main() {
val client = HttpClient(CIO)
val response: HttpResponse = client.get("https://ktor.io/")
println(response.status)
client.close()
}
但是,直接使用教程中的代码,我遇到了以下错误:
2023-07-10 11:45:24.674 [main] TRACE io.ktor.client.plugins.HttpPlainText - 在 https://ktor.io/ 中添加 Accept-Charset=UTF-8
2023-07-10 11:45:25.469 [DefaultDispatcher-worker-6] TRACE io.ktor.client.plugins.HttpCallValidator - 处理异常 kotlinx.coroutines.channels.ClosedReceiveChannelException: 通道已关闭以请求 https://ktor.io/
Exception in thread "main" kotlinx.coroutines.channels.ClosedReceiveChannelException: 通道已关闭
at kotlinx.coroutines.channels.BufferedChannel.getReceiveException(BufferedChannel.kt:1729)
at kotlinx.coroutines.channels.BufferedChannel.resumeWaiterOnClosedChannel(BufferedChannel.kt:2171)
at kotlinx.coroutines.channels.BufferedChannel.resumeReceiverOnClosedChannel(BufferedChannel.kt:2160)
...
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
Process finished with exit code 1
我尝试在不同的上下文中运行、更改 HTTP 动词、更改 URL、添加延迟以确保调用已完成,但都没有成功。
英文:
I'm trying to create an http client with ktor. Using the code found in the tutorial, I have this block,
suspend fun main() {
val client = HttpClient(CIO)
val response: HttpResponse = client.get("https://ktor.io/")
println(response.status)
client.close()
}
but unmodified from the tutorial, I'm getting this error,
2023-07-10 11:45:24.674 [main] TRACE io.ktor.client.plugins.HttpPlainText - Adding Accept-Charset=UTF-8 to https://ktor.io/
2023-07-10 11:45:25.469 [DefaultDispatcher-worker-6] TRACE i.k.client.plugins.HttpCallValidator - Processing exception kotlinx.coroutines.channels.ClosedReceiveChannelException: Channel was closed for request https://ktor.io/
Exception in thread "main" kotlinx.coroutines.channels.ClosedReceiveChannelException: Channel was closed
at kotlinx.coroutines.channels.BufferedChannel.getReceiveException(BufferedChannel.kt:1729)
at kotlinx.coroutines.channels.BufferedChannel.resumeWaiterOnClosedChannel(BufferedChannel.kt:2171)
at kotlinx.coroutines.channels.BufferedChannel.resumeReceiverOnClosedChannel(BufferedChannel.kt:2160)
at kotlinx.coroutines.channels.BufferedChannel.cancelSuspendedReceiveRequests(BufferedChannel.kt:2153)
at kotlinx.coroutines.channels.BufferedChannel.completeClose(BufferedChannel.kt:1930)
at kotlinx.coroutines.channels.BufferedChannel.isClosed(BufferedChannel.kt:2209)
at kotlinx.coroutines.channels.BufferedChannel.isClosedForSend0(BufferedChannel.kt:2184)
at kotlinx.coroutines.channels.BufferedChannel.isClosedForSend(BufferedChannel.kt:2181)
at kotlinx.coroutines.channels.BufferedChannel.completeCloseOrCancel(BufferedChannel.kt:1902)
at kotlinx.coroutines.channels.BufferedChannel.closeOrCancelImpl(BufferedChannel.kt:1795)
at kotlinx.coroutines.channels.BufferedChannel.close(BufferedChannel.kt:1754)
at kotlinx.coroutines.channels.ChannelCoroutine.close(ChannelCoroutine.kt)
at kotlinx.coroutines.channels.SendChannel$DefaultImpls.close$default(Channel.kt:98)
at io.ktor.network.tls.TLSClientHandshake$input$1.invokeSuspend(TLSClientHandshake.kt:91)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:100)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
Process finished with exit code 1
I have tried running in different contexts, changing the http verb, changing the url, adding sleep delays to ensure the call is being completed, all to no avail.
答案1
得分: 1
在这个情况下,这个错误的根本原因至少是一个代理问题。当我禁用了我的企业VPN时,请求成功完成了。
如果您遇到类似的情况,请测试一下,如果可以成功,然后您可以像这样为ktor客户端添加代理配置,
val client = HttpClient(CIO) {
engine {
proxy = ProxyBuilder.http("您公司提供的某个URL")
}
}
对于其他客户端引擎,请参阅ktor文档。
英文:
At least in this instance, the underlying cause of this error was a proxy issue. When I disabled my corporate VPN the request was able to complete successfully.
If you're encountering something similar, test if that works, then if it does you can add proxy configuration to the ktor client like so,
val client = HttpClient(CIO) {
engine {
proxy = ProxyBuilder.http("some_url_your_company_gives_you")
}
}
For other client engines see the ktor documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论