播放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.batchingexecutor $$ anonfun $$ 1.processbatch $ 1(BatchingExecutor.scala:67) [错误] akka.dispatch.batchingexecutor $$批次$$ anonfun $ $ $ 1.apply $ mcv $ sp(BatchingExecutor.scala:82) [错误] akka.dispatch.batchingexecutor $$批次$$ anonfun $ $ 1.apply(BatchingExecutor.scala:59) [错误] akka.dispatch.batchingexecutor $$批次$$ anonfun $ $ 1.apply(BatchingExecutor.scala:59) [错误] akka.dispatch.batchingexecutor $ batch.run(BatchingExecutor.scala:58) [错误] akka.dispatch.taskInvocation.Run(AbstractDispatcher.Scala:42) [错误] akka.dispatch.forkjoinexecutorconfigurator $ akkaforkjointask.exec(AbstractDispatcher.scala:386)
怎么了?
解决方案
拒绝响应数据有时腐败。
其他提示
不确定,但您可能需要将该代码与InputStreamReader一起包装:
不隶属于 StackOverflow