在IOS中使用HTTPS处理sendasynchronousrequest?

时间:2014-01-08 07:19:37

标签: ios http-post nsurlconnection

我有一个HTTPS请求。

我正在使用sendAsynchronousRequest的{​​{1}}。我不想用

NSURLConnection

当我使用NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; sendAsynchronousRequest时,每次我收到以下错误

NSURLConnection

这是我的代码

 NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9806)
 CFNetwork SSLHandshake failed (-9806)

这是用sendAsynchronousRequest调用NSURLConnection的委托方法的任何其他方法吗?

1 个答案:

答案 0 :(得分:0)

尝试添加此委托方法。希望它能为你效劳。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

        // Verify certificate:
        SecTrustResultType trustResult;
        OSStatus status = SecTrustEvaluate(challenge.protectionSpace.serverTrust, &trustResult);
        BOOL trusted = (status == errSecSuccess) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));

        if (trusted) {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
                 forAuthenticationChallenge:challenge];
        } else {
            if (1) {
                [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
                     forAuthenticationChallenge:challenge];
            } else {
                [challenge.sender cancelAuthenticationChallenge:challenge];
            }
        }
    } else {
        [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
    }
}