iphone-使用 CFNetwork API 使用自签名证书连接到 https url
简而言之,我的问题是我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论