【发布时间】:2015-05-13 00:56:45
【问题描述】:
我正在根据服务器名称处理服务器的 https 身份验证,但我想根据服务器给我的证书信任服务器。我该怎么做,有什么帮助吗??
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod
isEqualToString:NSURLAuthenticationMethodServerTrust])
{
// we only trust our own domain
if ([challenge.protectionSpace.host isEqualToString:@"www.serverpage.com"])
{
NSURLCredential *credential =
[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
}
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
我在网上搜索,发现大多数答案只是接受任何服务器身份验证而不验证。
【问题讨论】:
-
出于好奇,您为什么要自己实现 SSL 握手?你不能使用像 AFNetworking 这样的库吗?
-
我的应用没有 AFNetworking ,它只使用 NSURLConnection。
-
我的理解是
NSURLConnection会自己做这件事。检查服务器名称是默认 SSL 握手的一部分。还是您尝试使用自签名证书进行连接? -
是的,它的自签名证书检查
-
问题解决了吗?
标签: ios nsurlconnection nsurlconnectiondelegate