iOS AFNetworking https请求证书(验证证书,不校验证书)

在Xcode7.0之后,苹果废弃了NSURLConnection方法,数据请求使用NSURLSession,作为网络请求类第三方库使用量最大的AFN也及时的更新的新的版本——AFN 3.0版本。新的版本的里废弃了基于NSURLConnection封装的AFHTTPRequestOperationManager,转而使用基于NSURLSession封装的AFHTTPSessionManager了。

支持https(校验证书,不可以抓包):

// 1.初始化单例类

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

manager.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;

// 2.设置证书模式

NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];

NSData * cerData = [NSData dataWithContentsOfFile:cerPath];

manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:[[NSSet alloc] initWithObjects:cerData, nil]];

// 客户端是否信任非法证书

mgr.securityPolicy.allowInvalidCertificates = YES;

// 是否在证书域字段中验证域名

[mgr.securityPolicy setValidatesDomainName:NO];

支持https(不校验证书,可以抓包查看):

// 1.初始化单例类

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

// 2.设置非校验证书模式

manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];

manager.securityPolicy.allowInvalidCertificates = YES;

[manager.securityPolicy setValidatesDomainName:NO];

转载:www.jianshu.com/p/e727fc5d08ea

你可能感兴趣的:(iOS AFNetworking https请求证书(验证证书,不校验证书))