I am trying to construct a function in Swift that sends a http HEAD request to a specified url, and measures the response time from the server. I am not concerned with parsing the response, only that I get a 200 from the server. I can do this in python with the requests module:
import requests
def get_latency():
r = requests.head("http://example.com")
return r.elapsed.total_seconds()
I assume I will need to use NSURL for this, and I've been able to get this far, but can't figure out the best way to actually send the request...
let url = NSURL (string: "http://example.com")
let request = NSURLRequest(URL: url!)
let started = NSDate()
<<<Send http HEAD request, verify response>>> <- need help here
let interval = NSDate().timeIntervalSinceDate(started)