我使用Dispatch下拉了很多页面,这些页面我只需要前几个K,并且页面有时是千兆字节的。在scala-dispatch (分派/重启)中,或者在HTTP请求中,有没有办法截断接收到的正文?
(上下文:我正在从公共数据源读取CSV文件,我只是尝试获取字段名称(标题行)和一行示例数据。)
发布于 2015-03-26 13:03:57
您可以使用> handler,它允许您访问底层的com.ning.http.client.Response实例。从这里开始,它很简单:
import java.io._
import dispatch._, Defaults._
import com.ning.http.client.Response
def excerpt(bytes: Int) = {
response: Response =>
response.getResponseBodyExcerpt(100, "UTF-8")
}
def lines(count: Int) = {
response: Response =>
val stream = response.getResponseBodyAsStream
val reader = new BufferedReader(new InputStreamReader(stream))
Stream.continually(reader.readLine()).take(count).toList
}
val u = url("http://stackoverflow.com/")
Http(u > excerpt(100)).onComplete(println)
Http(u > lines(2)).onComplete(println)您还可以尝试使用Range header向服务器请求较小的字节间隔。这需要服务器支持,可以使用HEAD请求然后查找Accept-Ranges: bytes响应头进行测试。
https://stackoverflow.com/questions/29271125
复制相似问题