Ios 使用同步请求时,NSURErrorDomain代码=-1202

Ios 使用同步请求时,NSURErrorDomain代码=-1202,ios,objective-c,nsurlconnection,nsurlrequest,nsurlerrordomain,Ios,Objective C,Nsurlconnection,Nsurlrequest,Nsurlerrordomain,我尝试发送同步请求,但得到以下错误 错误域=NSURLErrorDomain Code=-1202“此域的证书 服务器无效。您可能正在连接到 假装是“xx.xxxx.com”,这会让你 有风险的信息。” 请在下面查找代码 NSURL *url=[[NSURL alloc]initWithString:strURL]; NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];

我尝试发送同步请求,但得到以下错误

错误域=NSURLErrorDomain Code=-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]];

如何解决此问题?

您可以使用异步版本的
NSURLConnection
方法(允许您指定委托),也可以使用来处理异步调用,就像处理同步调用一样

编辑:

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

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

(您需要有一个NSURLConnection*strong属性)。

我们没有在url请求中传递任何凭据。我尝试了一种休闲方式,但没有成功,当同步过程时间我将应用程序终止到后台时,如果我再次启动,那么我们将面临以下问题:NSURL*url=[[NSURL alloc]initWithString:strURL];NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:url];[请求设置HttpMethod:@“POST”];[请求setTimeoutInterval:90];[请求设置HttpBody:dataForChallenge];[请求设置值:@“应用程序/json”用于HttpHeaderField:@“内容类型”];[请求设置值:@“应用程序/json”用于HttpHeaderField:@“接受”];[NSURLConnection sendAsynchronousRequest:请求队列:[NSOperationQueue mainQueue]