【问题标题】:Okhttp. Exception java.io.IOException: unexpected end of stream on Connection好的http。异常 java.io.IOException:连接上的流意外结束
【发布时间】:2018-09-01 03:47:47
【问题描述】:

我遇到了一个问题,大约三分之一的 OkHttpClient newCall(request).execute(); 对服务器的请求因以下异常而失败:

Exception java.io.IOException: unexpected end of stream on Connection{[REDACTED_DOMAIN_NAME]:80, proxy=DIRECT@ hostAddress=[REDACTED_DOMAIN_NAME]/[REDACTED_IP_ADDRESS] cipherSuite=none protocol=http/1.1}

我禁用了证书验证。这种行为的原因可能是什么?

【问题讨论】:

  • 可能是服务器或网络问题。
  • @Okas,谢谢,确实是网络问题。原因是该服务器地址被俄罗斯国家监管机构列入黑名单(将超过 1450 万个 IP 地址列入黑名单,许多公司因此受到影响)

标签: android okhttp ioexception


【解决方案1】:

OkHttpClient 的默认请求协议是 HTTP2.0。 一些 Web 服务需要 HTTP1.1 请求。

将protocols(listOf(Protocol.HTTP_1_1)) 行添加到您的OkHttpClient。

示例;

val okHttpClient = OkHttpClient.Builder()
                    .protocols(listOf(Protocol.HTTP_1_1))
                    .readTimeout(40, TimeUnit.SECONDS)
                    .connectTimeout(40, TimeUnit.SECONDS)
                    .addInterceptor(loggingInterceptor)
                    .addInterceptor(authInterceptor)
                    .retryOnConnectionFailure(true)
                    .build()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    相关资源
    最近更新 更多