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);

    }];

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值