Https

作者: 英雄出少年 | 来源:发表于2016-06-23 23:51 被阅读34次

//
//  ViewController.m
//  11-掌握-HTTPS
//
//  Created by tangge on 15/7/15.
//  Copyright (c) 2015年 堂哥. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <NSURLSessionTaskDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
    
    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.apple.com/"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }];
    
    [task resume];
}

#pragma mark - <NSURLSessionTaskDelegate>
/**
 * challenge : 挑战、质询
 * completionHandler : 通过调用这个block,来告诉URLSession要不要接收这个证书
 */
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
{
    // 如果不是服务器信任类型的证书,直接返回
    if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) return;
    
    // void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *)
    // NSURLSessionAuthChallengeDisposition : 如何处理这个安全证书
    // NSURLCredential :安全证书
    
    // 根据服务器的信任信息创建证书对象
//    NSURLCredential *crdential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

    // 利用这个block说明使用这个证书
//    if (completionHandler) {
//        completionHandler(NSURLSessionAuthChallengeUseCredential, crdential);
//    }
    
    !completionHandler ? : completionHandler(NSURLSessionAuthChallengeUseCredential, challenge.proposedCredential);
}

@end


相关文章

  • https VS https

    HTTPS=SSL+HTTPHTTP协议传输的数据是未加密的 ,也就是明文,因此使用HTTP协议传输隐私信息非常不...

  • HTTPS

    什么是HTTPS HTTPS(全称:Hypertext Transfer Protocol over Secure...

  • HTTPS

    HTTP 有以下安全性问题: 使用明文进行通信,内容可能会被窃听;(请求时,需加密) 不验证通信方的身份,通信方的...

  • HTTPS

    站点证书的有效性 SSL 自身不要求用户检查Web服务器证书,但大部分现代浏览器都会对证书进行简单的完整性检查,并...

  • Https

    我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取。所以很多银行网站或电子邮箱等等安全级别较高的服务都会采...

  • HTTPS

    1.为什么要有HTTPS2.HTTPS的工作原理3.密码学4.HTTPS的优缺点5.在iOS中使用HTTPS 为什...

  • HTTPS

    精悍小文:https是如何工作的? - 简书 急速开发系列——打造完善的https使用方案 - 简书

  • https

    摘自[白话Https]https://www.cnblogs.com/xinzhao/p/4949344.html...

  • HTTPS

    一、背景 对于大规模的购物、银行事务或访问机密数据来说,这些重要的事务需要将 HTTP和数字加密技术结合起来使用,...

  • HTTPS

    在 HTTP 协议中有可能存在信息窃听或身份伪装等安全问题。使用 HTTPS 通信机制可以有效地防止这些问题。本篇...

网友评论

      本文标题:Https

      本文链接:https://www.haomeiwen.com/subject/pydodttx.html