我正在使用此代码从api中获取数据。但是会发生这样的错误。
import UIKit
import SwiftyJSON
import Alamofire
class ViewController: UIViewController {
@IBOutlet weak var tblview: UITableView!
var userName: [String] = []
var password: [String] = []
var orgId: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
makePostCall()
}
func makePostCall() {
let url1: String = "https://analyticsexample.com:30004/login"
print("step 1")
guard let url2 = URL(string: url1) else {
print("step 2")
print("Error: cannot create URL")
print("step 3")
return
}
var UrlRequest = URLRequest(url: url2)
print("step 4")
UrlRequest.httpMethod = "POST"
print("step 5")
let new: [String: Any] = ["userName": "usrname", "password": "psd", "orgId":"add"]
print("step 6")
let json: Data
print("step 7")
do {
print("step 8")
json = try JSONSerialization.data(withJSONObject: new, options: [])
print("step 9")
UrlRequest.httpBody = json
print("step 10")
} catch {
print("Error: cannot create JSON from todo")
return
}
let session = URLSession.shared
print("step 11")
let task = session.dataTask(with: UrlRequest) {
(data, response, error) in
guard error == nil else {
print("error calling POST on /todos/1")
print(error!)
return
}
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
print("step 12")
guard let received = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any] else {
print("Could not get JSON from responseData as dictionary")
return
}
print("The value is: " + received.description)
guard let value = received["id"] as? Int else {
print("Could not get value as int from JSON")
return
}
print("The ID is: \(value)")
} catch {
print("error parsing response from POST on value")
return
}
}
tblview.reloadData()
task.resume()
tblview.resignFirstResponder()
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping
(URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
print("step a")
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate) {
print("step b")
completionHandler(.rejectProtectionSpace, nil)
print("step c")
}
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
print("step d")
let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
print("step e")
completionHandler(.useCredential, credential)
print("step f")
}
}
错误:
TIC SSL Trust Error [1:0x604000167980]:3:0 2018-02-26 10:06:09.009173 + 0530任务[17851:504195] NSURLSession / NSURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9814)2018-02-26 10:06:09.009426 + 0530任务[17851:504195]任务 < 470AD92F-8974-46AF-9BCE-B99F646D6097>。&LT 1为卤素; HTTP加载失败(错误 代码:-1202 [3:-9814])2018-02-26 10:06:09.009897 + 0530 任务[470592F-8974-46AF-9BCE-B99F646D6097]任务[17851:504198]。< 1>。 完成错误 - 代码:-1202错误调用/ todos / 1上的POST