From 60b61184d6bdb61be229ad0af4bffb7c8e75d191 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 18 Dec 2021 20:37:09 +0800 Subject: [PATCH] =?UTF-8?q?post=E5=8F=AF=E4=BB=A5=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E8=AF=86=E5=88=ABcontentType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt | 9 ++++++++- .../main/java/io/legado/app/utils/StringExtensions.kt | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 81b11e51b..589b511d8 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -17,6 +17,8 @@ import io.legado.app.help.http.* import io.legado.app.model.ConcurrentException import io.legado.app.utils.* import kotlinx.coroutines.runBlocking +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response import java.net.URLEncoder import java.util.* @@ -207,7 +209,7 @@ class AnalyzeUrl( } } RequestMethod.POST -> body?.let { - if (!it.isJson()) { + if (!it.isJson() && !it.isXml()) { analyzeFields(it) } } @@ -391,8 +393,13 @@ class AnalyzeUrl( when (method) { RequestMethod.POST -> { url(urlNoQuery) + val contentType = headerMap["Content-Type"] + val body = body if (fieldMap.isNotEmpty() || body.isNullOrBlank()) { postForm(fieldMap, true) + } else if (!contentType.isNullOrBlank()) { + val requestBody = body.toRequestBody(contentType.toMediaType()) + post(requestBody) } else { postJson(body) } diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 1e57f6416..d026812e8 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -47,6 +47,12 @@ fun String?.isJsonArray(): Boolean = str.startsWith("[") && str.endsWith("]") } ?: false +fun String?.isXml(): Boolean = + this?.run { + val str = this.trim() + str.startsWith("<") && str.endsWith(">") + } ?: false + fun String.splitNotBlank(vararg delimiter: String): Array = run { this.split(*delimiter).map { it.trim() }.filterNot { it.isBlank() }.toTypedArray() }