身份验证挑战后未调用 URLSession 委托

URLSession delegates not called after authentication challenge

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

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

 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()

委托方法:

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 发送凭据服务器信任

我还看到了关于如何只能使用闭包或委托方法来检索两者的讨论 data/not。我已经尝试了一切但没有成功。

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

有两个问题:

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

        completionHandler(.PerformDefaultHandling, nil)