Domanda

MyRequest:

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

Codice utilizzato per decomprimere il 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
        }
}
.

Quindi invio due richieste allo stesso tempo.

.

A volte va bene

Ma a volte riproduce

[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 $ Esegui $ 1.Processbatch $ 1 (batchingexecutor.scala: 67) [ERRORE] AKKA.Dispatch.BatchingExecutor $ Batch $$ Anonfun $ Esegui $ 1.Altro $ MCV $ SP (batchingexecutor.scala: 82) [ERRORE] AKKA.Dispatch.BatchingExecutor $ Batch $$ Anonfun $ Esegui $ 1.Aggio (batchingexecutor.scala: 59) [ERRORE] AKKA.Dispatch.BatchingExecutor $ Batch $$ Anonfun $ Esegui $ 1.Aggio (batchingexecutor.scala: 59) [ERRORE] AKKA.Dispatch.Batchingexecutor $ Batch.Run (batchingexecutor.scala: 58) [Errore] akka.dispatch.taskinvocation.run (AbstractDispatcher.scala: 42) [Errore] Akka.Dispatch.ForkjoinexecutorConfigurator $ AKKAForkJointSak.exec (AbstractDispatcher.scala: 386)

Cosa c'è che non va?

È stato utile?

Soluzione

Risulta dei dati di risposta a volte è corrotto.

Altri suggerimenti

Non sono sicuro, ma potrebbe essere necessario avvolgere quel codice con un ingressoREAMReader:

https://stackoverflow.com/questions/3627401/Geditions/3627401/GZIPInputStream-String/3627441# 3627441

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top