1

任何人都可以帮助我了解如何使用 NSRULConnection 处理 https 请求吗?我已经阅读了很多教程和 Apple 文档。但我无法理解它是如何工作的。我已经实现了以下代表来处理 https 请求。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{    
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{       
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

当我实现上述委托时,我成功地得到了服务器的响应。谁能帮我知道这是如何工作的。委托中的每个参数是什么以及它在做什么?

提前致谢。

4

2 回答 2

2

protectionSpace视为服务器。委托方法connection: canAuthenticateAgainstProtectionSpace用于询问您是否可以处理服务器的身份验证要求。在您的情况下,您说“如果我们谈论的是 SSL 证书(这NSURLAuthenticationMethodServerTrust通常意味着),是的,我可以处理”。

然后,连接会要求您执行此操作connection:didReceiveAuthenticationChallenge并为此特定服务器提供 NSURLCredential。您可以使用credentialForTrust:存储在钥匙串中的信息来创建该服务器的证书。useCredential:forAuthenticationChallenge:最后告诉连接用这个凭证回答挑战,即使用钥匙串数据来验证证书。

于 2014-01-09T13:25:56.070 回答
0

这个例子将帮助你如何通过示例使用 iOS NSURLConnection

于 2014-01-09T13:12:59.093 回答