//
// ViewController.m
// 11-掌握-HTTPS
//
// Created by tangge on 15/7/15.
// Copyright (c) 2015年 堂哥. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <NSURLSessionTaskDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.apple.com/"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
}
#pragma mark - <NSURLSessionTaskDelegate>
/**
* challenge : 挑战、质询
* completionHandler : 通过调用这个block,来告诉URLSession要不要接收这个证书
*/
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
{
// 如果不是服务器信任类型的证书,直接返回
if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) return;
// void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *)
// NSURLSessionAuthChallengeDisposition : 如何处理这个安全证书
// NSURLCredential :安全证书
// 根据服务器的信任信息创建证书对象
// NSURLCredential *crdential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 利用这个block说明使用这个证书
// if (completionHandler) {
// completionHandler(NSURLSessionAuthChallengeUseCredential, crdential);
// }
!completionHandler ? : completionHandler(NSURLSessionAuthChallengeUseCredential, challenge.proposedCredential);
}
@end
Https
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 作者:又拍云原文地址:https://zhuanlan.zhihu.com/p/27395037 HTTPS(全称...
- 虽然Sublime Text不用购买注册码也能使用,但是使用过程中偶尔会弹出让购买的小窗口也不是很爽。记录一些搜集...
- node.js 和 nginx 支持https服务器 下面案例是:通过nginx添加证书代理的方式,实现ngin...
- 发晚了真是不好意思w ==============================================...