现在我使用 -connection:didReceiveAuthenticationChallenge: 函数来检查 SSL 证书。根据用户的要求,我需要检查所有的请求。但在我的测试演示中,-connection:didReceiveAuthenticationChallenge: delegate 函数将在 5 分钟内仅调用一次。 5 分钟后,它将再次调用。但是我们的用户可能会在 5 分钟内发送多个请求。有谁知道必须解决这个问题吗?
请求码
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_server_url]];
[urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];
urlConnection = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease] ;
[urlConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[urlConnection start];
委托(delegate)函数:
- (BOOL)connection:(NSURLConnection *)conn canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
NSLog(@"authenticate method:%@",protectionSpace.authenticationMethod);
return YES;
}
- (void) connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLAuthenticationChallenge *_challenge=[challenge retain];
SecTrustRef trustRef = [[_challenge protectionSpace] serverTrust];
SecTrustEvaluate(trustRef, NULL);
SecCertificateRef certRef = SecTrustGetCertificateAtIndex(trustRef, 0);
NSData *certificateData = (NSData *) SecCertificateCopyData(certRef);
const unsigned char *certificateDataBytes = (const unsigned char *)[certificateData bytes];
X509 *certificateX509 = d2i_X509(NULL, &certificateDataBytes, [certificateData length]);
NSString *subject = CertificateGetDomainName(certificateX509);
NSLog(@"Subject: %@", subject);
[[_challenge sender] continueWithoutCredentialForAuthenticationChallenge:_challenge];
}
最佳答案
没有刷新 TLS 缓存的简单方法:http://developer.apple.com/library/ios/#qa/qa1727/_index.html
尝试重新考虑您的用例以确定您是否真的每次都需要身份验证。
关于ios - 如何在 IOS 中检查所有 NSURLConnections 的 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14598968/