网络:NSURLSession 使用 HTTPS

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDelegate,NSURLSessionDataDelegate>
@property (nonatomic, strong) NSURLSession *session;// 自定义会话
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSURL *url = [NSURL URLWithString:@"https://mail.itcast.cn"];
    [[self.session dataTaskWithURL:url] resume];
}


//    NSURLSessionAuthChallengeUseCredential = 0, 使用(信任)证书
//    NSURLSessionAuthChallengePerformDefaultHandling = 1, 默认,忽略
//    NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2,   取消
//    NSURLSessionAuthChallengeRejectProtectionSpace = 3,      这次取消,下载次还来问

// 工作中直接复制这一段代理Ok了
// 金融公司
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
    NSLog(@"%@",challenge.protectionSpace);
    // 如果是请求证书信任,我们再来处理,其他的不需要处理
    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        NSURLCredential *cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        // 调用block
        completionHandler(NSURLSessionAuthChallengeUseCredential,cre);
    }
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}

//- (void)demo:(void(^)())block {
//    block();
//}

- (NSURLSession *)session {
    if (_session == nil) {
        _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    }
    return _session;
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NSURLSessionCorrectedResumeData是一个iOS开发中的类,用于处理网络请求的断点续传功能。 在网络请求过程中,由于各种原因(如网络不稳定或用户手动停止请求),请求可能会中断。为了方便用户继续中断的请求,苹果提供了NSURLSessionCorrectedResumeData来恢复中断的请求。 NSURLSessionCorrectedResumeData是一个二进制数据,它包含了中断请求的具体信息,如请求的URL、请求的方法、请求头信息、请求体等。开发者可以将这个数据保存到本地,以便在下次启动应用时重新发起请求。 当应用再次启动并需要继续中断的请求时,开发者可以使用NSURLSession的resumeData属性来读取之前保存的NSURLSessionCorrectedResumeData数据。 然后,开发者可以通过NSURLSession的downloadTask(withCorrectedResumeData:completionHandler:)方法来恢复请求。这个方法会根据传入的NSURLSessionCorrectedResumeData创建一个下载任务,并在下载完成后调用回调函数。 需要注意的是,正确使用NSURLSessionCorrectedResumeData需要遵循一些特定的规则。比如,只有之前使用NSURLSession的downloadTask方法发起的请求才能使用相关的恢复方法,并且不能保证100%的恢复成功。 总结来说,NSURLSessionCorrectedResumeData提供了一种方便的方式来处理网络请求中的断点续传功能,并且提高了用户体验。通过保存和恢复NSURLSessionCorrectedResumeData数据,开发者可以更加灵活地处理中断的请求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值