2

NSURLDownload を使用して、自己署名証明書を使用して Web サーバーからファイルをダウンロードしようとしています。これにより、通常、次の結果が得られます。

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9812)

または類似。

NSURLDownloadDelegate Protocol Referenceによると、認証中に次のメソッドが呼び出されることになっています。

- (BOOL)download:(NSURLDownload *)download canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
- (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

他のSOの回答で述べたように、これらの方法を使用して、自己署名証明書の使用を許可できます。残念ながら、彼らは呼ばれていません。

他のすべてのデリゲート メソッドは期待どおりに機能します。

簡略化されたコード (ここではあまり見ません):

- (int)retrieve:(NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    downloadComplete = false;
    downloadSucceeded = true;

    NSURLDownload *download = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self];
    if (!download) {
        fprintf(stderr, "Download failed\n");
    }

    while ((downloadComplete == false) && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

    return (downloadSucceeded == true);
}

- (BOOL)download:(NSURLDownload *)download canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    NSLog(@"download:canAuthenticateAgainstProtectionSpace");

    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"download:didReceiveAuthenticationChallenge");

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
        if([challenge.protectionSpace.host isEqualToString:@"myhost.mydomain.com"])
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

NSURLDownloadを支持して破棄することでこの問題を回避しましたNSURLConnectionが、何が起こっているのか知りたいです。(同等のNSURLConnectionDelegateメソッドが期待どおりに呼び出されます。)

NSURLDownload自己署名証明書を正常に使用した人はいますか?

4

0 に答える 0