码迷,mamicode.com
首页 > Web开发 > 详细

HTTPS 演练

时间:2016-01-01 22:59:58      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

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

    }

}

NSURLConnection HTTPS 访问

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

    NSURL *url = [NSURL URLWithString:@"https://baidu.com"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

 

    [NSURLConnection connectionWithRequest:request delegate:self];

}

 

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

 

    NSLog(@"%@", challenge);

    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {

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

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

 

        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];

    }

}

 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    [self.data setData:nil];

}

 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [self.data appendData:data];

}

 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

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

}

 

- (NSMutableData *)data {

    if (_data == nil) {

        _data = [NSMutableData data];

    }

    return _data;

}

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

    }];

}

HTTPS 演练

标签:

原文地址:http://www.cnblogs.com/fakeCoder/p/5093810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!