使用Alamofire访问本地主机API



我的url https://localhost:44301/api/authenticate斯威夫特5Alamofire 5 +

上面的本地主机正在处理我的swagger和禁用ssl的postman,我得到了正确的响应

private let session: Session = {
let manager = ServerTrustManager(evaluators: ["localhost:44301": DisabledTrustEvaluator()])
let configuration = URLSessionConfiguration.af.default
return Session(configuration: configuration, serverTrustManager: manager)
}()
//POST method call using Alamofire
let strURL = https://localhost:44301/api/authenticate
session.request(strURL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON {
...
//handle response
}

Error I get

Url : https://localhost:44301/api/Authenticate, Parameters : {"userNameOrEmailAddress":"admin","password":"123qwe"}
failure(Alamofire.AFError.serverTrustEvaluationFailed(reason: Alamofire.AFError.ServerTrustFailureReason.noRequiredEvaluator(host: "localhost")))
serverTrustEvaluationFailed(reason: Alamofire.AFError.ServerTrustFailureReason.noRequiredEvaluator(host: "localhost"))

我已经尝试了localhost而不是localhost:44301在ServerTrustManager,仍然相同的错误

您需要创建自定义会话

private let session: Session = {
// Create the server trust manager
let serverTrustManager = ServerTrustManager(evaluators: ["localhost": DisabledTrustEvaluator()])

// Create custom session
let configuration = URLSessionConfiguration.default
configuration.headers = .default
let man = Session(
configuration: URLSessionConfiguration.default,
serverTrustManager: serverTrustManager
)
return man
}()

之后
session.request(url).response { response in
switch response.result {
case let .success(data):
debugPrint("data = (data)")
case let .failure(error):
debugPrint("error = (error)")
}
}

相关内容

  • 没有找到相关文章

最新更新