首页 文章

Scala Dispatch简单获取请求

提问于
浏览
1

我试图用Scala Dispatch执行一个简单的GET请求,但是我错了404错误 . 意外的响应状态:404

这是一个有效的例子:

https://www.google.com/finance/info?infotype=infoquoteall&q=tsla,goog

但我对我的错误在我的代码中的位置感到惊讶

import dispatch._ , Defaults._  
object Main extends App {
  //concats a the proper uri together to send to google finance   
  def composeUri ( l:List[String]) = {   
    def google = host("google.com").secure
    def googleFinance = google / "finance" / "info" 
    def googleFinanceGet = googleFinance.GET
    val csv = l mkString "," 
    googleFinanceGet <<? Map("infotype"-> "infoquoteall", "q"->csv)   
  }   

  def sendRequest (uri:Req) = { 
    val res:Future[Either[Throwable,String]] = Http(uri OK as.String).either    
    res 
  } 
  val future  = sendRequest(composeUri(List("tsla","goog")))
  for (f <- future.left) yield println("There was an error" + f.getMessage) 
}

谢谢!

1 回答

  • 3

    如果您打印组合的URL(例如,使用 composeUri(List("tsla", "goog")).url ),则'll see that it'与您的工作示例不同 - 它不包括 www 子域 . 更改 google 的定义以使用 www.google.com ,这将按预期工作 .

相关问题