Created
September 30, 2010 21:23
-
-
Save zentrope/605348 to your computer and use it in GitHub Desktop.
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
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saving this for later reference.