【问题标题】:I just want to get an HTML page using spray我只想使用喷雾获取 HTML 页面
【发布时间】:2014-08-15 20:59:28
【问题描述】:

但他们的文档看起来假设我已经熟悉 Scala、Akka 和 Spray 本身。我的意思是我不知道如何做这个简单的基本事情,我很想在他们的主页上作为一个 sn-p 的代码......

我唯一能找到的是如何使用他们的spray-httpx 构建请求:

import spray.httpx.RequestBuilder._
val req = Get("http://url")

该对象没有将自己发送到任何地方的操作,所以我确定我应该使用 Akka 的东西来执行此操作,但他们的文档没有显示该过程。请告诉我该怎么做。如果spray-can 做同样的事情,我知道可以,我更喜欢这种方式。

【问题讨论】:

    标签: scala spray


    【解决方案1】:

    这里有一个例子:http://spray.io/documentation/1.1-SNAPSHOT/spray-client/

    import spray.http._
    import spray.client.pipelining._
    
    implicit val system = ActorSystem()
    import system.dispatcher // execution context for futures
    
    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
    
    val response: Future[HttpResponse] = pipeline(Get("http://spray.io/"))
    

    这里还有更简单的例子:https://github.com/spray/spray/wiki/spray-client

    val conduit = new HttpConduit("github.com")
    val responseFuture = conduit.sendReceive(HttpRequest(GET, uri = "/"))
    

    在这两种情况下,您都必须像处理 Future 一样处理结果,例如:

    for {response <- responseFuture} yield { someFunction(response) }
    

    【讨论】:

    • 我选择第一个,因为第二个看起来已弃用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多