ios - NSURLErrorDomain Code=-1202 使用同步请求时

标签 ios objective-c nsurlconnection nsurlrequest nsurlerrordomain

我尝试发送同步请求并收到以下错误。

Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xx.xxxx.com” which could put your confidential information at risk."

请在下面找到代码

    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]];

我该如何解决这个问题?

最佳答案

要么使用 NSURLConnection 方法的异步版本(允许您指定委托(delegate)),要么使用 trick like this像处理同步调用一样处理异步调用。

编辑:

关于使用 NSURLConnection 的异步版本,我的意思是:

self.connection = [[NSURLConnection alloc] initWithRequest:request
                                 delegate:self];

(你需要有一个 NSURLConnection* strong property)。

关于ios - NSURLErrorDomain Code=-1202 使用同步请求时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858835/

相关文章:

ios - 文件提供者应用程序扩展错误

objective-c - 提示在 cocoa 应用程序中进行根访问

ios - 无法在 ios 中使用 afnetworking 发布 JSON

iphone - NSURLConnection 委托(delegate)方法

ios - 从服务器下载pdf文件,保存在ApplicationSupport目录下。 iOS

ios - 加载后具有不同大小的 UIButton

iphone - 如何在 Safari 中打开 UIWebview 中的链接

ios - 当手机进入横向状态时,以编程方式更改自定义表格单元格中标签的约束。 ios 快速

ios - 在 UIViewController 中填充 UITableView 并使用 UISegmentedControl 更改数据源

c++ - 将 C++ 与 Objective-C 结合使用,如何修复 "Conflicting declaration ' typedef int BOOL'"?