我尝试发送同步请求并收到以下错误。
错误域=NSURLErrorDomain 代码=-1202 “此服务器的证书无效。您可能正在连接到伪装成“xx.xxxx.com”的服务器,这可能会使您的机密信息面临风险。”
请在下面找到代码
NSURL *url=[[NSURL alloc]initWithString:strURL];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];
[url release];
[request setTimeoutInterval:90];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:dataForChallenge];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: request returningResponse: &resp error: &err];
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// NSLog(@"selector : %@",NSStringFromSelector(_cmd));
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
// NSLog(@"selector 1: %@",NSStringFromSelector(_cmd));
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
// NSLog(@"selector : %@",NSStringFromSelector(_cmd));
if( [[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust] )
{
// NSLog(@"selector 1: %@",NSStringFromSelector(_cmd));
return YES;
}
return NO;
}
上述两种方法都没有被调用,在我的情况下,我只需要发送同步请求。
我使用了以下行,它工作正常,但 AppStore 拒绝了我的应用程序:
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
我怎样才能解决这个问题?