Scalaj.Http:仅在满足条件时才可以使用代理吗?

时间:2019-05-07 07:29:34

标签: scala proxy scalaj-http

在scala中,更精确地使用scalaj.http,有没有一种方法只有在满足条件时才使用代理?或者,有没有办法使用系统代理设置? 这是我的代码现在的样子的一个小例子:

val proxyHost = "www.myproxy.com"
val proxyPort = 1234
linkRequest = ("http://www.google.com", "http://www.stackoverflow.com", ...More links...)
val request0 = Http(linkRequest(0)).proxy(proxyHost, proxyPort)
val request1 = Http(linkRequest(1)).proxy(proxyHost, proxyPort)
val request2 = Http(linkRequest(2)).proxy(proxyHost, proxyPort)
val request3 = Http(linkRequest(3)).proxy(proxyHost, proxyPort)
val request4 = Http(linkRequest(4)).proxy(proxyHost, proxyPort)
.
.
.

现在,根据我们在哪里运行此代码,我可能需要或不需要代理设置。我尝试了以下方法,但没有成功:

1。

val proxyHost = ""
val proxyPort = 0

2。

val proxyHost = ""
val proxyPort = null

3。

val proxyHost = ""
val proxyPort = 80

我想避免这样的事情:

val useProxy = Random.nextBoolean()
val proxyHost = "www.myproxy.com"
val proxyPort = 1234
linkRequest = ("http://www.google.com", "http://www.stackoverflow.com", ...More links...)
if(useProxy){
  val request0 = Http(linkRequest(0)).proxy(proxyHost, proxyPort)
  val request1 = Http(linkRequest(1)).proxy(proxyHost, proxyPort)
  val request2 = Http(linkRequest(2)).proxy(proxyHost, proxyPort)
  val request3 = Http(linkRequest(3)).proxy(proxyHost, proxyPort)
  val request4 = Http(linkRequest(4)).proxy(proxyHost, proxyPort)
  .
  .
  .
}
else{
  val request0 = Http(linkRequest(0))
  val request1 = Http(linkRequest(1))
  val request2 = Http(linkRequest(2))
  val request3 = Http(linkRequest(3))
  val request4 = Http(linkRequest(4))
  .
  .
  .
}

赞成不重复执行代码的解决方案

谢谢

0 个答案:

没有答案