利用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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章