gpt4 book ai didi

Scala 的 Source.fromURL 通过 http 代理

转载 作者:行者123 更新时间:2023-12-01 09:35:35 25 4
gpt4 key购买 nike

我无法通过 http 代理从 Scala 中的 URL 读取。

我有这个代码

//This is just to print the environment
import scala.collection.JavaConversions._
println(System.getProperties.filter(_._1 startsWith("http")).toList sortBy(_._1) mkString "\n")

import scala.io.Source
val html = Source.fromURL("http://google.com")
val s = html.mkString
println(s)

我收到以下错误
(http.proxyHost,158.169.9.13)
(http.proxyPassword,*****)
(http.proxyPort,8012)
(http.proxyUser,*****)
java.io.IOException: Server returned HTTP response code: 407 for URL: http://google.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1612)
at java.net.URL.openStream(URL.java:1035)
at scala.io.Source$.fromURL(Source.scala:143)
at scala.io.Source$.fromURL(Source.scala:133)
at Main$$anon$1.<init>(download.scala:7)
at Main$.main(download.scala:1)
at Main.main(download.scala)

我期待 google.com 页面源的打印。

最佳答案

你试过 Source.fromURL(new java.net.URL("myURL")) API 吗?代码看起来像这样:

 val response: String = try {
val proxy: Proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080))
val connection: HttpURLConnection = new java.net.URL("myURL").openConnection(proxy).asInstanceOf[HttpURLConnection]
connection.connect()
Source.fromInputStream(connection.getInputStream).getLines.mkString
}
catch {
case e: Throwable => "" // Do whatever you want here: logging/throw exception/..
}

关于Scala 的 Source.fromURL 通过 http 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21464358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com