iOS之使用NSURLConnection连接HTTPS(SSL)站点
2015-01-19 12:49
369 查看
转载自:http://www.tuicool.com/articles/7FnIZv
使用 NSURLConnection 连接HTTPS站点,需要处理SSL认证, NSURLConnectionDelegate 中定义了一些方法来处理认证
–
connection:canAuthenticateAgainstProtectionSpace:
–
connection:didReceiveAuthenticationChallenge:
使用 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站点的几种方法
使用 NSURLConnection 连接HTTPS站点,需要处理SSL认证, NSURLConnectionDelegate 中定义了一些方法来处理认证
–
connection:canAuthenticateAgainstProtectionSpace:
–
connection:didReceiveAuthenticationChallenge:
一. 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站点的几种方法
相关文章推荐
- apache中使用mod_gnutls模块实现多个SSL站点配置(多个HTTPS协议的虚拟主机)
- CURL组建中,HTTPS使用ip地址连接时候,SSL安全验证方法!
- iOS之AFNetworking3.x使用自签名证书建立https连接
- iOS UIWebView 加载https 站点出现NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL,
- iOS 使用NSURLConnection连接服务器和JSON作为数据交换方式
- OpenSSL使用1(用OpenSSL生成自签名证书在IIS上搭建Https站点)(用于iOS的https访问)
- HTTPS在SSL/TLS上的使用和配置(IIS8和iOS客户端)
- 调用Https WebService发布后使用时报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系”证书验证失败的解决过程(3)
- delphi2007下使用indy连接https时could not load ssl library问题的解决
- apache中使用mod_gnutls模块实现多个SSL站点配置(多个HTTPS协议的虚拟主机)
- 使用SVCUTIL生成客户端代理类时:元数据包含无法解析的引用:“https: 无法为 SSL/TLS 安全通道与颁发机构“localhost”建立信任关系。基础连接已经关闭: 未能为 SSL/TL
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
- [转]C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现
- C#、VB.NET使用HttpWebRequest访问https地址(SSL)的实现方法
- 调用Https WebService是报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系”的解决过程
- 在IIS中使用SSL配置HTTPS网站(转)