Advertisement
wandrake

Untitled

Nov 26th, 2013
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.74 KB | None | 0 0
  1. object RESTConnectohr {
  2.   def doGET(
  3.     url_string: String,
  4.     vars: Option[Map[String, String]] = None,
  5.     headers: Option[Map[String, String]] = None): Pair[Int, String] = {
  6.  
  7.     val get_params: String = vars match {
  8.       case Some(q) => q
  9.         .map((x: (String, String)) => x._1 + "=" + URLEncoder.encode(x._2, "ISO-8859-1"))
  10.         .mkString("&")
  11.       case _ => ""
  12.     }
  13.  
  14.     val url: URL = new URL(url_string + get_params)
  15.  
  16.     val conn: HttpURLConnection = url
  17.       .openConnection()
  18.       .asInstanceOf[HttpURLConnection]
  19.     conn.setRequestMethod("GET")
  20.  
  21.     headers.map(
  22.       _.map((x: (String, String)) => conn.setRequestProperty(x._1, x._2))
  23.     )
  24.  
  25.     (conn.getResponseCode(), conn.getResponseMessage())
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement