afn https网络访问

NSURLSession 的 HTTPS 访问


/**

 接收到身份质询 Challenge


 身份质询保存在受保护空间内!


 * 完成回调参数

 - NSURLSessionAuthChallengeDisposition


    NSURLSessionAuthChallengeUseCredential = 0,                 使用指定的凭据

    NSURLSessionAuthChallengePerformDefaultHandling = 1,        对身份质询的默认处理

    NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2, 取消请求,忽略凭据参数

    NSURLSessionAuthChallengeRejectProtectionSpace = 3,         本次忽略质询

 - NSURLCredential 证书

 */

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {


    NSLog(@"%@", challenge.protectionSpace);

    // 判断身份质询方式是否是信任证书

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {


        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];


        // 回调信任受保护空间中的身份质询

        completionHandler(NSURLSessionAuthChallengeUseCredential, credential);

    }

}


AFN的https网络访问

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.securityPolicy.allowInvalidCertificates = YES;

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];


    [manager GET:@"https://mail.itcast.cn" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {

        NSLog(@"%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

        NSLog(@"%@", error);

    }];

}




你可能感兴趣的:(网络)