gpt4 book ai didi

ios - 身份验证挑战后未调用 URLSession 委托(delegate)

转载 作者:可可西里 更新时间:2023-11-01 00:58:11 29 4
gpt4 key购买 nike

我正在对服务器进行后调用,但我一直收到服务器证书无效的错误消息,因此在研究之后我发现我必须使用身份验证质询来信任服务器。这一切都很好(我打印了身份验证挑战以确保它确实如此)但是其余的调用没有通过并且我无法从我的服务器获取信息,身份验证挑战后没有任何反应,通常我希望被返回数据。但是什么也没有发生,我的 urlsessiondatadelegates 都没有被调用(检查中断)。

谁能看出这是为什么?代码:

 var request = URLRequest(url: URL(string: "https://45.56.105.97:7915/search-v2")!)
request.httpMethod = "POST"
request.addValue("Basic ***********", forHTTPHeaderField: "Authorization")

let task = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
task.dataTask(with: request) { (data, response, error) in
print(error)
print(data)
if let responseData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
print("data: \(responseData)")
self.parseXML(data!)

}
}.resume()

委托(delegate)方法:

func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")

if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
print("send credential Server Trust")
let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
challenge.sender!.use(credential, for: challenge)

}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
print("send credential HTTP Basic")
let defaultCredentials: URLCredential = URLCredential(user: "username", password: "password", persistence:URLCredential.Persistence.forSession)
challenge.sender!.use(defaultCredentials, for: challenge)

}else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
print("send credential NTLM")

} else{
challenge.sender!.performDefaultHandling!(for: challenge)
}

}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
print("Data received: \(data)")
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {

print("Response received: \(response)")
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print("something went wrong: \(error)")
}

func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
print("rep: \(response)")
}

运行后,我得到的打印结果是:

是否 autherntcationchallenge = NSURLAuthenticationMethodServerTrust发送凭据服务器信任

我也看到过关于如何只能使用闭包或委托(delegate)方法来检索数据/不能同时使用两者的对话。我已经尝试了一切但没有成功。

**另外我知道我不应该只是盲目地信任服务器,但我试图弄清楚为什么我无法接收回数据,我知道这存在严重的安全漏洞。

最佳答案

有两个问题:

  • 正如 Rob 在评论中提到的那样,您没有在任何委托(delegate)方法中调用完成处理程序。这意味着连接将在那时停止取得进展。
  • 您正在调用质询发送方的方法来提供凭据。这将适用于 NSURLConnection,但 NSURLSession 要求您通过调用完成处理程序来完成它。否则,事情会非常糟糕,IIRC,例如

        completionHandler(.PerformDefaultHandling, nil)

关于ios - 身份验证挑战后未调用 URLSession 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40196064/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com