gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:30:50 30 4
gpt4 key购买 nike

从 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com