关于AFN 适配HTTPS配置及忽略证书方法

在实战开发过程中,通常使用HTTPS实现网络请求,有时候后台开发人员无法给你提供https验证证书,此时需要通过忽略(跳过)证书验证的方法去实现网络请求,下面是实现方法:
1.忽略证书配置方法

_sessionManager = [AFHTTPSessionManager manager];
    _sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
    _sessionManager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    _sessionManager.securityPolicy.allowInvalidCertificates = YES;
    [_sessionManager.securityPolicy setValidatesDomainName:NO];

注意:如果请求的URL里面需要去掉端口号,否则会请求失败。
2.配置证书验证

+ (void)setSecurityPolicyWithCerPath:(NSString *)cerPath validatesDomainName:(BOOL)validatesDomainName {
   NSData *cerData = [NSData dataWithContentsOfFile:cerPath];
   AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
   securityPolicy.allowInvalidCertificates = YES;
   securityPolicy.validatesDomainName = validatesDomainName;
   securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerData, nil];
   [_sessionManager setSecurityPolicy:securityPolicy];
}

你可能感兴趣的:(关于AFN 适配HTTPS配置及忽略证书方法)