4
我一直在使用HTTP進行開發。下面的代碼在使用HTTP與開發服務器連接時效果很好。但是,當我將方案更改爲https時,它不會將成功的https帖子發送到服務器。如何使用NSURLSession在swift中發送HTTPS POST請求
我還需要做什麼才能從HTTP POST切換到HTTPS POST?
class func loginRemote(successHandler:()->(), errorHandler:(String)->()) {
let user = User.sharedInstance
// this is where I've been changing the scheme to https
url = NSURL(String: "http://url.to/login.page")
let request = NSMutableURLRequest(URL: url)
let bodyData = "email=\(user.email)&password=\(user.password)"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
request.HTTPMethod = "POST"
let session = NSURLSession.sharedSession()
// posting login request
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
// email+password were good
successHandler()
} else {
// email+password were bad
errorHandler("Status: \(httpResponse.statusCode) and Response: \(httpResponse)")
}
} else {
NSLog("Unwrapping NSHTTPResponse failed")
}
})
task.resume()
}