Skip to content

Instantly share code, notes, and snippets.

@zentrope
Created September 30, 2010 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zentrope/605348 to your computer and use it in GitHub Desktop.
Save zentrope/605348 to your computer and use it in GitHub Desktop.
private class Http(val address: String) {
// Simple class for snagging content from and posting content
// to a URL. No error handling.
import java.net._
import java.io._
import scala.io._
def get(): String = {
val url: URL = new URL(address)
Source.fromInputStream(url.openStream()).getLines.mkString
}
def post(data: String): String = {
val url: URL = new URL(address)
val conn: URLConnection = url.openConnection()
conn.setRequestProperty("Content-Type", "application/json")
conn.setDoOutput(true)
val writer = new OutputStreamWriter(conn.getOutputStream())
writer.write(data)
writer.flush()
// TODO: Detect error (based on code), then use err stream
// to get message, if any
val content = Source.fromInputStream(conn.getInputStream())
.getLines.mkString
writer.close()
return content
}
}
@zentrope
Copy link
Author

Saving this for later reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment