Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created March 31, 2018 08:45
Show Gist options
  • Save xuwei-k/0c87b9aa94154999d4f3ddefaf0fafe6 to your computer and use it in GitHub Desktop.
Save xuwei-k/0c87b9aa94154999d4f3ddefaf0fafe6 to your computer and use it in GitHub Desktop.
scala-native http get example
scalaVersion := "2.11.12"
enablePlugins(ScalaNativePlugin)
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.3.7")
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