From 8edc4b0282b367bdd78b45f3d1266e894371624c Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 18 Apr 2022 15:50:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9B=A0=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=86=99=E5=BC=95=E5=8F=91=E7=9A=84user=20agent=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/http/HttpHelper.kt | 15 +++++++++------ .../java/io/legado/app/help/http/OkHttpUtils.kt | 7 ------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index c4d8f1f1c..07a7dd7a6 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -1,5 +1,6 @@ package io.legado.app.help.http +import io.legado.app.constant.AppConst import io.legado.app.help.config.AppConfig import io.legado.app.help.http.cronet.CronetInterceptor import io.legado.app.help.http.cronet.CronetLoader @@ -36,12 +37,14 @@ val okHttpClient: OkHttpClient by lazy { .followSslRedirects(true) .addInterceptor(Interceptor { chain -> val request = chain.request() - .newBuilder() - .addHeader("Keep-Alive", "300") - .addHeader("Connection", "Keep-Alive") - .addHeader("Cache-Control", "no-cache") - .build() - chain.proceed(request) + val builder = request.newBuilder() + if (request.header(AppConst.UA_NAME) == null) { + builder.addHeader(AppConst.UA_NAME, AppConfig.userAgent) + } + builder.addHeader("Keep-Alive", "300") + builder.addHeader("Connection", "Keep-Alive") + builder.addHeader("Cache-Control", "no-cache") + chain.proceed(builder.build()) }) if (!AppConfig.isGooglePlay && AppConfig.isCronet && CronetLoader.install()) { builder.addInterceptor(CronetInterceptor(null)) diff --git a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt index f51094a26..12835aaf9 100644 --- a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt +++ b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt @@ -1,7 +1,5 @@ package io.legado.app.help.http -import io.legado.app.constant.AppConst -import io.legado.app.help.config.AppConfig import io.legado.app.utils.EncodingDetect import io.legado.app.utils.GSON import io.legado.app.utils.Utf8BomUtils @@ -25,7 +23,6 @@ suspend fun OkHttpClient.newCallResponse( ): Response { return withContext(IO) { val requestBuilder = Request.Builder() - requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) requestBuilder.apply(builder) var response: Response? = null for (i in 0..retry) { @@ -94,10 +91,6 @@ fun ResponseBody.text(encode: String? = null): String { fun Request.Builder.addHeaders(headers: Map) { headers.forEach { - if (it.key == AppConst.UA_NAME) { - //防止userAgent重复 - removeHeader(AppConst.UA_NAME) - } addHeader(it.key, it.value) } }