Skip to content

Instantly share code, notes, and snippets.

@mpen
Created October 29, 2009 05:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpen/221181 to your computer and use it in GitHub Desktop.
Save mpen/221181 to your computer and use it in GitHub Desktop.
Apache HttpComponents HttpClient Get
// Apache HttpComponents HttpClient Get
import java.io.{ File, IOException }
import java.net.{ URI, UnknownHostException, URLDecoder }
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.{ HttpResponse, HttpEntity, HttpStatus }
import org.apache.http.util.EntityUtils
import org.apache.commons.io.{ FileUtils, FilenameUtils }
object ApacheHttpClientGet {
def main(args: Array[String]) :Unit = {
val pages = List(
"http://www.yubin-nenga.jp/design_kit/hagaki_temp.html",
"https://www.google.com/accounts/Login?hl=ja",
"http://www.yahoo.co.jp/",
"http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8",
)
val client = new DefaultHttpClient
pages.foreach { page =>
try {
val uri = new URI(page)
val get = new HttpGet(uri)
val response: HttpResponse = client.execute(get)
if (response.getStatusLine.getStatusCode == HttpStatus.SC_OK) {
val decodedPath = URLDecoder.decode(uri.getPath, "UTF-8")
var fileName = FilenameUtils.getName(decodedPath)
if (fileName == "") fileName = "index.html"
val file = new File("test-" + fileName)
val ba = EntityUtils.toByteArray(response.getEntity)
FileUtils.writeByteArrayToFile(file, ba)
}
} catch {
case e: UnknownHostException => println(e)
case e: IOException => println(e)
}
}
client.getConnectionManager.shutdown
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment