简体   繁体   English

OkHttp不会重用HTTPS / HTTP2连接

[英]OkHttp doesn't reuse HTTPS/HTTP2 connections

Each time I make a new request, a new connection is open. 每次我发出新请求时,都会打开一个新连接。 There isn't any connection reuse whatsoever. 没有任何连接重用。 What am I doing wrong? 我究竟做错了什么?

I am using the latest OkHttp package (3.11.0). 我正在使用最新的OkHttp软件包(3.11.0)。 My server is supporting HTTP/2. 我的服务器支持HTTP / 2。 The request URLs are all HTTPS. 请求URL均为HTTPS。

    OkHttpClient client = new OkHttpClient();
    try {
        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();
        mJSONstring = response.body().string();
    } catch (IOException e) {
        e.printStackTrace();
    }

I found the reason: I was instantiating a new client each time. 我找到了原因:我每次都实例化一个新客户端。

If you want to reuse connections, you should use the same client instance for all the calls. 如果要重用连接,则应对所有调用使用相同的客户端实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM