iphone-使用 CFNetwork API 使用自签名证书连接到 https url

发布于 2024-12-19 02:31:22 字数 3511 浏览 1 评论 0原文

简而言之,我的问题是我尝试使用 NSURLConnection 类及其委托方法连接到 https url。

我收到“发生 SSL 错误,无法与服务器建立安全连接”错误

所以我实现了 这个解决方案。但运气不佳。解决方案中指定的委托方法永远不会被调用。

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
method

被调用并出现上述错误

所以我转而使用 CFNetwork API 通过自签名证书连接到 https url

这是我的代码。我从 此处CFNetwork 编程指南 来自 Apple

CFReadStreamRef readStream;
            CFHTTPMessageRef request;

            CFStringRef requestMessage = (CFStringRef)postparameters;

            request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("POST"),
                                                 (CFURLRef) postURL, kCFHTTPVersion1_1);


            CFHTTPMessageSetBody(request, (CFDataRef) requestMessage);
            readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);
            CFRelease(request);

            NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                      [NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
                                      [NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
                                      [NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
                                      kCFNull,kCFStreamSSLPeerName,
                                      nil];

            CFReadStreamSetProperty((CFReadStreamRef)readStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);

            CFReadStreamOpen(readStream);

            /* Add to the run loop */
            CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(),
                                            kCFRunLoopCommonModes);

            if (!CFReadStreamOpen(readStream)) {
            DLog(@"method called");
            [_progressAlert dismissWithClickedButtonIndex:0 animated:YES];
            CFReadStreamSetClient(readStream, 0, NULL, NULL);
            CFReadStreamUnscheduleFromRunLoop(readStream,
                                              CFRunLoopGetCurrent(),
                                              kCFRunLoopCommonModes);
            CFRelease(readStream);
        }
        else {
        CFHTTPMessageRef myResponse =
        (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream,
                                                   kCFStreamPropertyHTTPResponseHeader);
        //You can get the complete status line from the response message by calling the function CFHTTPMessageCopyResponseStatusLine:
        CFStringRef myStatusLine = CFHTTPMessageCopyResponseStatusLine(myResponse);
        DLog(@"status: %@", (NSString *)myStatusLine);

        UInt32 myErrCode = CFHTTPMessageGetResponseStatusCode(myResponse);
        NSLog(@"error :%lu", myErrCode);
        }

输出 i在控制台中看到的是

2011-11-30 21:02:07.720 [3800:f803][Line 180] method called

我需要添加到上面的代码中才能使其工作。

我猜这与身份验证有关。

请帮我一些示例代码。

更新:我更新了上面的代码,response.My !CFReadStreamOpen(readStream) 方法返回 true 并且打印日志

Briefly my problem is i have tried to connect to https url using NSURLConnection class and it s delegate methods.

I was getting "An SSL Error has occured and a secure connection to the server could not be made" error

So i implemented this solution.but no luck.Those delegate methods specified in the solution never gets called.

only

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
method

gets called with the above error

So i shifted to use CFNetwork API to connect to https url with a self signed certificate

And here is my code.I took it from here and CFNetwork programming guide from apple

CFReadStreamRef readStream;
            CFHTTPMessageRef request;

            CFStringRef requestMessage = (CFStringRef)postparameters;

            request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("POST"),
                                                 (CFURLRef) postURL, kCFHTTPVersion1_1);


            CFHTTPMessageSetBody(request, (CFDataRef) requestMessage);
            readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);
            CFRelease(request);

            NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                      [NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
                                      [NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
                                      [NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
                                      kCFNull,kCFStreamSSLPeerName,
                                      nil];

            CFReadStreamSetProperty((CFReadStreamRef)readStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);

            CFReadStreamOpen(readStream);

            /* Add to the run loop */
            CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(),
                                            kCFRunLoopCommonModes);

            if (!CFReadStreamOpen(readStream)) {
            DLog(@"method called");
            [_progressAlert dismissWithClickedButtonIndex:0 animated:YES];
            CFReadStreamSetClient(readStream, 0, NULL, NULL);
            CFReadStreamUnscheduleFromRunLoop(readStream,
                                              CFRunLoopGetCurrent(),
                                              kCFRunLoopCommonModes);
            CFRelease(readStream);
        }
        else {
        CFHTTPMessageRef myResponse =
        (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream,
                                                   kCFStreamPropertyHTTPResponseHeader);
        //You can get the complete status line from the response message by calling the function CFHTTPMessageCopyResponseStatusLine:
        CFStringRef myStatusLine = CFHTTPMessageCopyResponseStatusLine(myResponse);
        DLog(@"status: %@", (NSString *)myStatusLine);

        UInt32 myErrCode = CFHTTPMessageGetResponseStatusCode(myResponse);
        NSLog(@"error :%lu", myErrCode);
        }

The output i see in the console is

2011-11-30 21:02:07.720 [3800:f803][Line 180] method called

What i need to add to my above code to make it work.

I guess it is something related to authentication.

Please help me with some sample code.

Update: I updated my code above and the response.My !CFReadStreamOpen(readStream) method returns true and the log gets printed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文