Question

myrequest:

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

code utilisé pour décompresser le corps

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

Puis j'envoie deux demandes en même temps.

Parfois, tout va bien

mais parfois il reproche

[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 $ Run $ 1.ProcessBatch 1 $ (BatchingExecutor.Scala: 67) [ERREUR] AKKA.DISPATCH.BATCHINGINGINGEXECUNTURE $ BATCH $$ ANONFUN $ Run $ 1.Apply $ MCV $ SP (BatchingExecutor.scala: 82) [ERREUR] AKKA.DISPATCH.BATCHINGINGINGEXECUNTURE $ BATCH $$ anonfun $ Run $ 1.Apply (BatchingExecutor.scala: 59) [ERREUR] AKKA.DISPATCH.BATCHINGINGINGEXECUNTURE $ BATCH $$ anonfun $ Run $ 1.Apply (BatchingExecutor.scala: 59) [ERREUR] AKKA.DISPATCH.BatchingExecutor $ Batch.Run (BatchingExecutor.scala: 58) [Erreur] akka.dispatch.taskinvocation.run (abstractdispatcher.scala: 42) [ERREUR] AKKA.DISPATCH.FORKJOINEXECUTUNConfigurateur $ AKKAFORKJOVASK.EXEC (AbstractDisPatcher.Scala: 386)

Qu'est-ce qui ne va pas?

Était-ce utile?

La solution

éteint que les données de réponse sont parfois corrompues.

Autres conseils

Pas sûr, mais vous devrez peut-être envelopper ce code avec un INPUTStreamReader:

https://stackoverflow.com/questions/3627401/gzipInputStream-a-string/3627441# 3627441

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top