문제

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 $ batch $$ anonfun $ 실행 $ 1.ProcessBatch $ 1 (batchingexecutor.scala : 67) [오류] akka.dispatch.batchingexecutor $ batch $$ anonfun $ 실행 $ 1.Apply $ mcv $ sp (BatchingExecutor.Scala : 82) [오류] akka.dispatch.batchingexecutor $ batch $$ anonfun $ 실행 $ 1.Apply (batchingexecutor.scala : 59) [오류] akka.dispatch.batchingexecutor $ batch $$ 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로 랩해야 할 수도 있습니다.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top