1

同期リクエストを送信しようとすると、以下のエラーが発生しました。

エラー 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;
}

上記の 2 つのメソッドは呼び出されません。私の場合、同期要求のみを送信する必要があります。

以下の行を使用しましたが、正常に動作しますが、AppStore は私のアプリを拒否しました:

    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

どうすればこれを修正できますか?

4

1 に答える 1

1

メソッドの非同期バージョンNSURLConnection(デリゲートを指定できる) を使用するか、このようなトリックを使用して非同期呼び出しを同期呼び出しのように処理します。

編集:

の非同期バージョンの使用についてNSURLConnection、私が意味したことは次のとおりです。

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

(NSURLConnection* の強力なプロパティが必要です)。

于 2013-12-31T15:42:50.290 に答える