利用NSURLSession实现https请求
2016-09-23 16:36
477 查看
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
/*
https原理:
1,客户端请求服务器,如果是第一次请求,服务器返回向客户端返回证书
2,客户端需要处理是否同意安装证书,如果同意安装,以后的所有通信都需要用这个证书来加密。(手机端需要自动处理证书)
3,服务器拿到数据以后,利用自己的私钥解密数据。(数据只有私钥才能解密)
*/
//1,不带证书的请求,有时候不用安装,原因有二:可能以前装过,或者有些大网站不用安装
// NSURLSessionTask *task = [[NSURLSession sharedSession]dataTaskWithURL:[NSURL URLWithString:@"https://developer.apple.com/"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// //
//
// NSLog(@"error:%@",error);
// NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
// }];
// [task resume];
//2,程序自动安装证书的方式
NSURLSession *sesson = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
NSURLSessionTask *task = [sesson dataTaskWithURL:[NSURL URLWithString:@"https://xxx/json"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//
NSLog(@"error:%@",error);
NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
}
#pragma mark -----NSURLSessionTaskDelegate-----
//NSURLAuthenticationChallenge 中的protectionSpace对象存放了服务器返回的证书信息
//如何处理证书?(使用、忽略、拒绝。。)
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler//通过调用block,来告诉NSURLSession要不要收到这个证书
{
//(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
//NSURLSessionAuthChallengeDisposition (枚举)如何处理这个证书
//NSURLCredential 授权
//证书分为好几种:服务器信任的证书、输入密码的证书 。。,所以这里最好判断
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){//服务器信任证书
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];//服务器信任证书
if(completionHandler)
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
NSLog(@"....completionHandler---:%@",challenge.protectionSpace.authenticationMethod);
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
/*
https原理:
1,客户端请求服务器,如果是第一次请求,服务器返回向客户端返回证书
2,客户端需要处理是否同意安装证书,如果同意安装,以后的所有通信都需要用这个证书来加密。(手机端需要自动处理证书)
3,服务器拿到数据以后,利用自己的私钥解密数据。(数据只有私钥才能解密)
*/
//1,不带证书的请求,有时候不用安装,原因有二:可能以前装过,或者有些大网站不用安装
// NSURLSessionTask *task = [[NSURLSession sharedSession]dataTaskWithURL:[NSURL URLWithString:@"https://developer.apple.com/"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// //
//
// NSLog(@"error:%@",error);
// NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
// }];
// [task resume];
//2,程序自动安装证书的方式
NSURLSession *sesson = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
NSURLSessionTask *task = [sesson dataTaskWithURL:[NSURL URLWithString:@"https://xxx/json"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//
NSLog(@"error:%@",error);
NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
}
#pragma mark -----NSURLSessionTaskDelegate-----
//NSURLAuthenticationChallenge 中的protectionSpace对象存放了服务器返回的证书信息
//如何处理证书?(使用、忽略、拒绝。。)
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler//通过调用block,来告诉NSURLSession要不要收到这个证书
{
//(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
//NSURLSessionAuthChallengeDisposition (枚举)如何处理这个证书
//NSURLCredential 授权
//证书分为好几种:服务器信任的证书、输入密码的证书 。。,所以这里最好判断
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){//服务器信任证书
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];//服务器信任证书
if(completionHandler)
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
NSLog(@"....completionHandler---:%@",challenge.protectionSpace.authenticationMethod);
}
相关文章推荐
- 利用NSURLSession实现https请求
- 利用NSURLSession实现https请求
- 利用信号量实现NSURLSession的同步请求
- linux下利用https实现站点访问
- 利用JQuery实现更简单的Ajax跨域请求
- 利用jQuery实现更简单的Ajax跨域请求
- java利用线程池实现处理socket请求的小例子
- 利用script标签实现的跨域名AJAX请求(ExtJS)
- 利用https实现站点访问
- 利用JQuery实现更简单的Ajax跨域请求
- 利用O’Reilly公司的cos实现文件上传前获取请求参数
- 利用JQuery+Ajax实现aspx页面无刷新异步请求
- 利用asp.net 2.0构建企业级门户平台(2、实现页面请求的调度)
- 利用https实现站点访问
- 利用HTTPS实现站点访问
- 利用script标签实现的跨域名AJAX请求(ExtJS)
- .net2005和2003下进行https请求的不同实现
- 1.1和2.0下进行https请求的不同实现
- 利用struts2-ssl-plugin实现https安全链接
- 关于JQuery 中$.ajax函数利用jsonp实现Ajax跨域请求ASP.NET的WebService成功获取数据的案例