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));
}
}
Scala:how to write a simple HTTP GET request client in Scala (with a timeout)