Pergunta

Meu pedido:

WS.url(url)
.withHeaders("Accept-Encoding" -> "gzip")
.withQueryString("xxx","xxx")

Código usado para descomprimir o corpo

 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
        }
}

Então envio duas solicitações ao mesmo tempo.

Às vezes está tudo bem

Mas às vezes isso reprova

[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 $ batch $$ anonfun $ execução $ 1.processbatch $ 1 (batchingExecutor.scala: 67) [erro] akka.dispatch.batchingexecutor $ batch $$ anonfun $ run $ 1.Apply $ mcv $ spatchingExecutor.scal 82) [Erro] akka.dispatch.batchingExecutor $ batch $$ anonfun $ run $ 1.Apply (batchingExecutor.scala: 59) [Erro] akka.dispatch.batchingexecutor $ batch $$ anonfun $ run $ 1.Apply (batchingExecut.scala: 59) [Erro] akka.dispatch.batchingExecutor $ batch.run (batchingExecutor.scala: 58) [Erro] akka.dispatch.taskinvocation.run (abstractdispatcher.scala: 42) [error] akka.dispatch.ForkJOineXecutrofornfigur: 42. (AbstractDispatcher.scala: 386)

O que está errado ?

Foi útil?

Solução

Acontece que os dados de resposta às vezes estão corrompidos.

Outras dicas

Não tenho certeza, mas pode ser necessário agrupar esse código com um InputStreamReader:

https://stackoverflow.com/questions/3627401/gzipinputstream-to-string/3627441#3627441

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top