Scala CookBook: http://scalacookbook.com/
@throws(classOf[java.io.IOException]) @throws(classOf[java.net.SocketTimeoutException]) def get(url: String, connectTimeout:Int =5000, readTimeout:Int =5000, requestMethod: String = "GET") = { import java.net.{URL, HttpURLConnection} val connection = (new URL(url)).openConnection.asInstanceOf[HttpURLConnection] connection.setConnectTimeout(connectTimeout) connection.setReadTimeout(readTimeout) connection.setRequestMethod(requestMethod) val inputStream = connection.getInputStream val content = io.Source.fromInputStream(inputStream).mkString if (inputStream != null) inputStream.close content } if want to set the request property, add:for (header <- headers) { val headerDetails = header.split(":") if (headerDetails.size<=2) { connection.setRequestProperty(headerDetails(0), headerDetails(1)); }}
时间: 2024-10-07 04:28:33