Created
March 31, 2018 08:45
-
-
Save xuwei-k/0c87b9aa94154999d4f3ddefaf0fafe6 to your computer and use it in GitHub Desktop.
scala-native http get example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scalaVersion := "2.11.12" | |
enablePlugins(ScalaNativePlugin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.3.7") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import java.net._ | |
import java.io._ | |
object ScalaNativeHttpGetExample { | |
def using[A <: Closeable, B] (a: A)(f: A => B): B = | |
try { | |
f(a) | |
} finally { | |
a.close() | |
} | |
def main (args: Array[String]): Unit = { | |
val host = "repo1.maven.org" | |
val port = 80 | |
// val path = "/maven2/org/scalaz/scalaz-core_2.12/7.2.20/scalaz-core_2.12-7.2.20.pom" | |
val path = args.head | |
using(new Socket(host, port)) { socket => | |
using(new BufferedReader(new InputStreamReader(socket.getInputStream))) { reader => | |
using(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream))) { writer => | |
writer.write("GET " + path + " HTTP/1.0\r\n") | |
writer.write("Host: " + host + ":" + port + "\r\n") | |
writer.write("\r\n") | |
writer.flush() | |
@annotation.tailrec | |
def loop (): Unit = { | |
val line = reader.readLine | |
if (line != null) { | |
println(line) | |
loop() | |
} | |
} | |
loop() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment