在Scala Dispatch中解码流式GZIP响应?
发布时间:2020-12-16 10:03:54 所属栏目:安全 来源:网络整理
导读:从API接收Gzip响应,但Dispatch 0.9.5似乎没有任何方法来解码响应.有任何想法吗? 这是我当前的实现,println只打印出字节的字符串表示. Http( host("stream.gnip.com") .secure .addHeader("Accept-Encoding","gzip") / gnipUrl as.stream.Lines(println))()
从API接收Gzip响应,但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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |