使用AFN框架來訪問不受信任的https網(wǎng)站
當(dāng)我們使用AFN框架向一個不受信任的https網(wǎng)站發(fā)出請求時必須做一些配置與操作才能保證不出錯收苏,首先我們要禁用ATS特性无畔,然后:
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//01 創(chuàng)建會話管理者
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//設(shè)置反序列化方法
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 驗證方式 AFSSLPinningModeNone:最不嚴(yán)格龟劲,只要是信任機(jī)構(gòu)簽發(fā)的證書就會通過洋只,若是自己服務(wù)器生成的證書在這里無法通過驗證
// AFSSLPinningModePublicKey:只驗證公鑰
// AFSSLPinningModeCertificate:最嚴(yán)格:除了驗證公鑰外益缠,還會驗證域名/有效期等信息
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
//設(shè)置允許不受信任的證書
securityPolicy.allowInvalidCertificates = YES;
//設(shè)置不驗證域名
securityPolicy.validatesDomainName = NO;
manager.securityPolicy = securityPolicy;
//02 發(fā)送GET請求
[manager GET:@"https://kyfw.12306.cn/otn" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"success--%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"failure--%@",error);
}];
}
@end