【问题标题】:NSURLErrorDomain Code=-1202 when using Syncronous requestNSURLErrorDomain Code=-1202 使用同步请求时
【发布时间】:2013-12-31 14:36:34
【问题描述】:

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

Error Domain=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]];

我该如何解决这个问题?

【问题讨论】:

  • 我们没有在 url 请求中传递任何凭据。

标签: ios objective-c nsurlconnection nsurlrequest nsurlerrordomain


【解决方案1】:

您可以使用异步版本的 NSURLConnection 方法(允许您指定委托)或使用 trick like this 来处理异步调用,就像它是同步调用一样。

编辑:

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

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

(你需要有一个 NSURLConnection* 强属性)。

【讨论】:

  • 我尝试了休闲方式,但没有运气,当同步过程时间我将应用程序杀死到后台然后如果我再次启动,那么我们将面临问题:
  • NSURL *url=[[NSURL alloc]initWithString:strURL]; NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url]; [请求 setHTTPMethod:@"POST"]; [请求 setTimeoutInterval:90]; [请求 setHTTPBody:dataForChallenge]; [请求 setValue:@"application/json" forHTTPHeaderField:@"content-type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
猜你喜欢
  • 1970-01-01
  • 2017-05-04
  • 2014-11-11
  • 2015-01-25
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多