使用派遣0.9.5落后代理?

时间:2013-01-10 08:23:34

标签: scala proxy scala-dispatch

我正在尝试执行(在IntelliJ IDE或sbt命令行中)来自代理后面的这个非常基本的调度片段:

import dispatch._
val svc = url("http://api.hostip.info/country.php")
val country = Http(svc > as.String)
println(country())

我能得到的只是一个例外:

java.net.ConnectException: Connection timed out: no further information to
    http://api.hostip.info/country.php java.util.concurrent.ExecutionException:
       java.net.ConnectException: Connection timed out: no further information 
            to http://api.hostip.info/country.php

我尝试了没有确定的结果来设置通常的vm参数: -Dhttp.proxyHost= _ my_proxy_host_ -Dhttp.proxyPort=80 并且仍然有相同的例外。

另一方面,以下代码段效果很好:

import dispatch._
val svc = url("http://api.hostip.info/country.php") setProxyServer(new com.ning.http.client.ProxyServer(myproxyhost,80))
val country = Http(svc > as.String)
println(country())

因为它看起来不太美观,也不像scala-ish,我想知道在这种情况下它是不是我应该做的。

提前感谢任何帮助。

2 个答案:

答案 0 :(得分:8)

如果您设置此参数,将使用

http.proxyHosthttp.proxyPort

-Dcom.ning.http.client.AsyncHttpClientConfig.useProxyProperties=true

另外还有参数:

-Dcom.ning.http.client.AsyncHttpClientConfig.proxy.user=user
-Dcom.ning.http.client.AsyncHttpClientConfig.proxy.password=password
-Dcom.ning.http.client.AsyncHttpClientConfig.proxy.protocol=NTLM

答案 1 :(得分:4)

看起来我的问题不是很鼓舞人心。

我在 pimp my library 风格中做了一些练习:

package dispatch

package object ext {
    import com.ning.http.client.ProxyServer

  class DefaultPoxyVerbs(override val subject: Req) extends  ProxyVerbs

  object NoProxy extends ProxyServer("",0)

  object Proxy {
    def apply(host: String,port: Int) = new ProxyServer(host,port)
  }

  trait ProxyVerbs extends RequestVerbs {

    def through(proxy : ProxyServer) =
      if (NoProxy == proxy) subject else subject.setProxyServer(proxy)

    def ||>(proxy : ProxyServer) = through(proxy)

  }

  implicit def implyProxyVerbs(builder: Req) =
    new DefaultPoxyVerbs(builder)
}

现在我可以写:

  import dispatch._
  import dispatch.ext._

  val svc = url("http://api.hostip.info/country.php") through Proxy("blah blah blah",80)

  val country = Http(svc > as.String)
  println(country())
对于调度api风格,这是一个更令人愉悦和连贯的。

虽然这是一项有趣的练习,但我现在还不知道我最初是否按照预期的方式使用api,也不知道为什么设置http.proxyHosthttp.proxyPort属性不起作用因为它似乎适用于others

相关问题