利用NSURLSession实现https请求

  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     // Do any additional setup after loading the view from its nib.  
  4.       
  5.     /* 
  6.       
  7.      https原理: 
  8.        1,客户端请求服务器,如果是第一次请求,服务器返回向客户端返回证书 
  9.        2,客户端需要处理是否同意安装证书,如果同意安装,以后的所有通信都需要用这个证书来加密。(手机端需要自动处理证书) 
  10.        3,服务器拿到数据以后,利用自己的私钥解密数据。(数据只有私钥才能解密) 
  11.       
  12.      */  
  13.   
  14.   
  15.       
  16.    //1,不带证书的请求,有时候不用安装,原因有二:可能以前装过,或者有些大网站不用安装  
  17. //    NSURLSessionTask *task = [[NSURLSession sharedSession]dataTaskWithURL:[NSURL URLWithString:@"https://developer.apple.com/"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {  
  18. //        //  
  19. //          
  20. //        NSLog(@"error:%@",error);  
  21. //        NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);  
  22. //    }];  
  23. //    [task resume];  
  24.       
  25.       
  26.     //2,程序自动安装证书的方式  
  27.     NSURLSession *sesson = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];  
  28.   
  29.     NSURLSessionTask *task = [sesson dataTaskWithURL:[NSURL URLWithString:@"https://xxx/json"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {  
  30.         //  
  31.           
  32.         NSLog(@"error:%@",error);  
  33.         NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);  
  34.     }];  
  35.     [task resume];  
  36. }  
  37.   
  38.   
  39. #pragma mark -----NSURLSessionTaskDelegate-----  
  40. //NSURLAuthenticationChallenge 中的protectionSpace对象存放了服务器返回的证书信息  
  41. //如何处理证书?(使用、忽略、拒绝。。)  
  42. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler//通过调用block,来告诉NSURLSession要不要收到这个证书  
  43. {  
  44.    //(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler  
  45.     //NSURLSessionAuthChallengeDisposition (枚举)如何处理这个证书  
  46.     //NSURLCredential 授权  
  47.       
  48.     //证书分为好几种:服务器信任的证书、输入密码的证书  。。,所以这里最好判断  
  49.       
  50.     if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){//服务器信任证书  
  51.           
  52.         NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];//服务器信任证书  
  53.         if(completionHandler)  
  54.            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);  
  55.     }  
  56.   
  57.       
  58.     NSLog(@"....completionHandler---:%@",challenge.protectionSpace.authenticationMethod);  
  59.       
  60. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值