iOS之使用NSURLConnection连接HTTPS(SSL)站点
来源:互联网 发布:淘宝客服要注意的事项 编辑:程序博客网 时间:2024/06/07 07:07
转载自:http://www.tuicool.com/articles/7FnIZv
使用 NSURLConnection 连接HTTPS站点,需要处理SSL认证, NSURLConnectionDelegate 中定义了一些方法来处理认证
一. NSURLConnection 中处理SSL
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{ return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];}
如果接受任何证书
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];}
如果使用证书验证
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ static CFArrayRef certs; if (!certs) { NSData*certData =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"srca" ofType:@"cer"]]; SecCertificateRef rootcert =SecCertificateCreateWithData(kCFAllocatorDefault,CFBridgingRetain(certData)); const void *array[1] = { rootcert }; certs = CFArrayCreate(NULL, array, 1, &kCFTypeArrayCallBacks); CFRelease(rootcert); // for completeness, really does not matter } SecTrustRef trust = [[challenge protectionSpace] serverTrust]; int err; SecTrustResultType trustResult = 0; err = SecTrustSetAnchorCertificates(trust, certs); if (err == noErr) { err = SecTrustEvaluate(trust,&trustResult); } CFRelease(trust); BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed)||(trustResult == kSecTrustResultConfirm) || (trustResult == kSecTrustResultUnspecified)); if (trusted) { [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; }else{ [challenge.sender cancelAuthenticationChallenge:challenge]; }}
二. AFNetworking 框架中处理SSL
使用 AFURLConnectionOperation 类的下面两个方法,分别将上述代码以block方式传入即可。
– setAuthenticationAgainstProtectionSpaceBlock:
– setAuthenticationChallengeBlock:
参考:
Technical Note TN2232 – HTTPS Server Trust Evaluation
NSURLConnection Class Reference
NSURLConnectionDelegate Protocol Reference
How to use NSURLConnection to connect with SSL for an untrusted cert?
NSURLConnection with Self-Signed Certificates
iPhone SSL based NSURLConnection with your own root cert
dhoerl / MyWebFetcher.m
https://github.com/AFNetworking/AFNetworking/
AFNetworking – AFURLConnectionOperation Class Reference
关于在UIwebView中访问HTTPS站点的几种方法
- iOS之使用NSURLConnection连接HTTPS(SSL)站点
- 网络:NSURLConnection 使用 HTTPS
- NSURLConnection实现HTTPS(SSL)链接请求
- iOS UIWebView 加载https 站点出现NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL,
- iOS UIWebView 加载https 站点出现NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL,
- https(ssl)连接之python实现
- https(ssl)连接之python实现
- https(ssl)连接之python实现
- https(ssl)连接之python实现
- IOS网络开发之如何使用NSURLConnection
- iOS网络开发之NSURLConnection的使用
- (0036) iOS 开发之HTTPS、SSL验证
- IOS之NSURLConnection
- iOS 网络之NSURLConnection
- iOS之NSURLConnection
- iOS: NSUrlConnection & SSL, Http connection frameworks
- IOS Https ssl加密
- Nginx 配置 SSL 证书 + HTTPS 站点小记
- 利用Nginxcp为cPanel/WHM服务器开启nginx支持
- 第一次页面正常,提交后验证失败,重新回到表单页乱码
- 数据结构与算法(一)概论
- 隐藏未处理的模版
- 64 bit Ubuntu support 32 bit binary
- iOS之使用NSURLConnection连接HTTPS(SSL)站点
- weblogic10.0客户端,必须要生成wlfullclient.jar
- java访问URL并下载文件
- iOS内IPC(进程间通信)方法小结
- 数据结构与算法(二)线性表
- css优先级总结(转)
- ubuntu myeclipse svn
- openlayers2.X画园形和扇形demo
- 广州汉藏文化是如何赚取客户前期费用的?