Играть WS выполнить два (или более) GZIP-запроса одновременно иногда
-
21-12-2019 - |
Вопрос
myrequest:
WS.url(url)
.withHeaders("Accept-Encoding" -> "gzip")
.withQueryString("xxx","xxx")
.
код, используемый для распаковки тела
def call[T](api: WeiboApi[T])(implicit mf: Manifest[T]) = {
val param = parameters(api)
(api match {
case _: Get[T] => get(api.url, param)
case _: Post[T] => post(api.url, param)
}) map {
resp =>
try {
val decompressedBody = decompressIfGzip(resp)
api.parse(decompressedBody)
} catch {
case e: WeiboApiError => throw e
case e: Exception =>
throw new Exception("cannot parse body api " + api, e)
}
}
}
private def decompressIfGzip(resp: Response) = {
val ahcResp = resp.getAHCResponse
ahcResp.getHeader("Content-Encoding") match {
case "gzip" | "GZIP" =>
val in = ahcResp.getResponseBodyAsStream
val gzipStream = new GZIPInputStream(in)
try {
val source = scala.io.Source.fromInputStream(gzipStream)
source.mkString
} finally {
in.close()
}
case _ =>
ahcResp.getResponseBody
}
}
.
Затем я отправляю два запроса одновременно.
Иногда это хорошо
Но иногда это отрывок
[error] Corrupt GZIP trailer
[error] sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
[error] sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
[error] sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
[error] lib.weibo.Weibo$.lib$weibo$Weibo$$decompressIfGzip(Weibo.scala:138)
[error] lib.weibo.Weibo$$anonfun$call$1.apply(Weibo.scala:47)
[error] lib.weibo.Weibo$$anonfun$call$1.apply(Weibo.scala:42)
[error]
.
akka.dispatch.batchingeexecuture $ batch $$ anonfun $ run $ 1. processbatch $ 1 (batchingexecuture.scala: 67) [Ошибка] akka.dispatch.batchingexecutors $ batch $$ anonfun $ run $ 1.apply $ mcv $ sp (batchingexecuture.scala: 82) [Ошибка] akka.dispatch.batchingexucter $ batch $$ anonfun $ run $ 1.apply (BatchingexecuteCuteCution.scala: 59) [Ошибка] akka.dispatch.batchingexucter $ batch $$ anonfun $ run $ 1.apply (BatchingexecuteCuteCution.scala: 59) [Ошибка] akka.dispatch.batchingexecutors $ batch.run (batchingexececuture.scala: 58) [Ошибка] akka.dispatch.taskinvocation.run (AbstraldissiSpatcher.scala: 42) [Ошибка] akka.dispatch.forkjoeaexecutureConsfigurator $ akkaforkjointask.exec (Abstraldispatcher.scala: 386)
Что не так?
Решение
Оказывается, данные ответа иногда повреждены.
Другие советы
Не уверен, но вам может потребоваться обернуть этот код с помощью ввода ввода:
https://stackoverflow.com/questions/3627401/gzipinputstream-to-tom-string/3627441# 3627441