在OkHTTP中,如何将keep-alive设置为false



OkHttpClient的默认行为是将keep-alive设置为true。对于我们的特定用例,我需要将其设置为false。

我在Java源代码中找不到任何设置来执行此操作。

有人碰到过这个吗?

我在OkHttpClient 3.8.1 上

感谢

根据https://github.com/square/okhttp/issues/2031

你可以运行类似的演示代码

okHttpClient.setConnectionPool(new ConnectionPool(0, 0));

或者添加类似Connection:close的标头。

为您提供此行为线索的源代码是:https://github.com/square/okhttp/blob/480c20e46bb1745e280e42607bbcc73b2c953d97/okhttp/src/main/kotlin/okhttp3/internal/connection/RealConnectionPool.kt#L108

最新更新