@interface ViewController () <NSURLSessionTaskDelegate>
@property (nonatomic, strong) NSURLSession *session;
@end
@implementation ViewController
- (NSURLSession *)session {
if (_session == nil) {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
}
return _session;
}
- (void)viewDidLoad {
[super viewDidLoad];
//https://kyfw.12306.cn/otn/
NSURL *url = [NSURL URLWithString:@"https://kyfw.12306.cn/otn/"];
[[self.session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",html);
}] resume];
}
//代理的方法
//接受服务器的挑战,信任服务器
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(0,credential);
}
}
@end
iOS NSURLSession HTTPS
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- HTTPS简单说明: HTTPS(全称HyperTextTransferProtocoloverSecureSoc...
- 把client.p12导入进入项目,添加测试代码 实现NSURLSessionDelegate 这里主要完成的就是...
- 网络请求AFNetworking使用量不用过多的说了,但是在开发过程中,需要用到自己去封装一个网络请求,此处主要是...
- 前段是时间做项目要求进行双向认证,网上的查了很多都是不全的,今天将我整理之后的分享给大家。 废话不多说直接上最主要...