2014-01-30 2 views
3

http 프록시를 통해 스칼라에서 URL을 읽을 수 없습니다.http 프록시를 통한 스칼라의 Source.fromURL

는 나는 다음과 같은 오류

(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 페이지 소스의 인쇄를 기다리고 있었다를 얻을이 코드

//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) 

에게 있습니다.

답변

1

Source.fromURL (새 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/.. 
    }