超过 30 秒的 connectTimeout 对 OKHTTP 不起作用 [英] connectTimeout more than 30 seconds does not work for OKHTTP

查看:254
本文介绍了超过 30 秒的 connectTimeout 对 OKHTTP 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 connectTimeout 设置为 2 分钟,但仅在 30 秒后尝试仍然失败.

I am trying to set connectTimeout to 2 minutes, but still the tries fails after 30 seconds only.

我的客户是这样的:

client = new OkHttpClient.Builder()
            .callTimeout(2, TimeUnit.MINUTES)
            .connectTimeout(2, TimeUnit.MINUTES)
            .writeTimeout(2, TimeUnit.MINUTES)
            .readTimeout(2, TimeUnit.MINUTES)
            .build();

public Call callReq(Callback callback) {


    Request.Builder builder = new Request.Builder()
            .url("http://203.0.113.1");

    //Create request builder
    final Request request = builder
            .build();

    Call call = client.newCall(request);

    call.enqueue(callback);
    return call;
}

我在 30 秒内收到的此呼叫的失败响应.我想让它等 2 分钟.如果我将超时减少到 15 秒,它会起作用.

The failure response of this call I am receiving in 30 seconds. I wanted that to wait for 2 minutes. It works if I reduce the timeout to 15 seconds.

推荐答案

我怀疑您遇到了一些额外的平台超时,例如我的测试在 1:15 后超时.

I suspect you are hitting some additional platform timeouts, e.g. my test timed out after 1:15.

package okhttp3.curl

import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.logging.LoggingEventListener
import java.util.concurrent.TimeUnit

fun main() {
  val listener = LoggingEventListener.Factory()
  val client = OkHttpClient.Builder()
      .eventListenerFactory(listener)
      .callTimeout(2, TimeUnit.MINUTES)
      .connectTimeout(2, TimeUnit.MINUTES)
      .writeTimeout(2, TimeUnit.MINUTES)
      .readTimeout(2, TimeUnit.MINUTES)
      .build()
  val response: Response = client.newCall(
        Request.Builder().url("http://203.0.113.1").build()).execute()

  println(response.request.url)
}

输出

Mar 29, 2020 12:33:58 PM okhttp3.internal.platform.Platform log
INFO: [72 ms] connectStart: /203.0.113.1:80 DIRECT
Mar 29, 2020 12:35:14 PM okhttp3.internal.platform.Platform log
INFO: [75599 ms] connectFailed: null java.net.ConnectException: Failed to connect to /203.0.113.1:80
Mar 29, 2020 12:35:14 PM okhttp3.internal.platform.Platform log
INFO: [75601 ms] callFailed: java.net.ConnectException: Failed to connect to /203.0.113.1:80
Exception in thread "main" java.net.ConnectException: Failed to connect to /203.0.113.1:80

这篇关于超过 30 秒的 connectTimeout 对 OKHTTP 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆