scala - 在 Scala Dispatch 中解码流式 GZIP 响应?

标签 scala scala-dispatch asynchttpclient

从 API 接收 Gzipped 响应,但 Dispatch 0.9.5 似乎没有任何方法来解码响应。有任何想法吗?

这是我当前的实现,println只打印出字节的字符串表示。

   Http(
      host("stream.gnip.com")
      .secure
      .addHeader("Accept-Encoding", "gzip")
       / gnipUrl
      > as.stream.Lines(println))()

试图查看实现我自己的处理程序,但不确定从哪里开始。这是 Lines 的相关文件:https://github.com/dispatch/reboot/blob/master/core/src/main/scala/as/stream/lines.scala

谢谢!

最佳答案

干脆放弃了 Dispatch,直接使用 Java API。令人失望,但它完成了工作。

  val GNIP_URL = isDev match {
    case true => "https://url/apath/track/dev.json"
    case false => "https://url/path/track/prod.json"
  }
  val GNIP_CHARSET = "UTF-8"

  override def preStart() = {
    log.info("[tracker] Starting new Twitter PowerTrack connection to %s" format GNIP_URL)

    val connection = getConnection(GNIP_URL, GNIP_USER, GNIP_PASSWORD)
    val inputStream = connection.getInputStream()
    val reader = new BufferedReader(new InputStreamReader(new StreamingGZIPInputStream(inputStream), GNIP_CHARSET))
    var line = reader.readLine()
    while(line != null){
        println(line)
        line = reader.readLine()
    }
  }

  private def getConnection(urlString: String, user: String, password: String): HttpURLConnection = {
    val url = new URL(urlString)

    val connection = url.openConnection().asInstanceOf[HttpURLConnection]
    connection.setReadTimeout(1000 * 60 * 60)
    connection.setConnectTimeout(1000 * 10)

    connection.setRequestProperty("Authorization", createAuthHeader(user, password));
    connection.setRequestProperty("Accept-Encoding", "gzip")
    connection
  }

  private def createAuthHeader(username: String, password: String) = {
    val encoder = new BASE64Encoder()
    val authToken = username+":"+password
   "Basic "+encoder.encode(authToken.getBytes())
  }

使用 GNIP 的例子:https://github.com/gnip/support/blob/master/Premium%20Stream%20Connection/Java/StreamingConnection.java

关于scala - 在 Scala Dispatch 中解码流式 GZIP 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15800393/

相关文章:

python - Spark 和 PySpark 之间是否存在功能对等

scala - 排序 `Future` s 超时

python - 使用tornado.httpclient.AsyncHTTPClient 访问重定向响应上的 header ?

java - 调度失败,代理需要身份验证 : Remotely Closed Connection

scala - 带有可变参数的案例类的隐式 jsonFormat

scala - 排序HList

scala - 将 paytm 回调 url 设置为本地主机

scala - 返回准确的响应/ header ?

使用 AsyncHttpClient 的 java.net.UnknownHostException