【发布时间】:2014-01-17 06:09:46
【问题描述】:
我想允许带有 NSURLConnection 的自签名证书,前提是主机在受信任的列表中。
我看到很多人在做这样的事情:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if (allowSelfSignedCertForThisHost) {
NSLog(@"Allowing self signed!");
return YES;
}
}
return NO;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if ([trustedHosts containsObject:challenge.protectionSpace.host]) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
但是,我想知道您为什么要调用 useCredential:forAuthenticationChallenge,但之后还要调用 continueWithoutCredentialForAuthenticationChallenge。
【问题讨论】:
标签: objective-c ssl nsurlconnection