Https访问

作者: 泰克2008 | 来源:发表于2017-07-28 15:19 被阅读5次

有的时候Https访问我们可以直接拿到数据!!
因为有的公司财大气粗!哥么有钱!!可以认证!!!
认证过的Https访问时强制安装证书的!!
但是很多公司的Https访问你这样是拿不到数据的!!

代码实现

@interface ViewController ()<NSURLSessionTaskDelegate>

@end

@implementation ViewController

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

#pragma mark - <协议>
/**
 参数:
 Challenge :询问,询问客户端是否需要信任来自服务器的证书!
 属性:protectionSpace(受保护的空间)
 Auth-Scheme:NSURLAuthenticationMethodServerTrust  服务器返回的授权模式:要求信任服务器的证书
 completionHandler : 通过代码块回调,决定对证书的处理!!
 
 NSURLSessionAuthChallengeDisposition (处置):
 NSURLSessionAuthChallengeUseCredential                     
        -  使用服务器发回证书(保存在challenge里面)
 NSURLSessionAuthChallengePerformDefaultHandling
        -  默认处理方式,会忽略证书
 NSURLSessionAuthChallengeCancelAuthenticationChallenge
        -  取消整个请求,忽略证书
 NSURLSessionAuthChallengeRejectProtectionSpace
        -  本次拒绝,下次再试
 
 NSURLCredential   证书
 */
-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
    //1.判断服务器的身份验证的方法是否是:信任服务器证书
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSLog(@"哥么是信任的证书");
            //2.获得证书
        NSURLCredential * credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            //3.对服务器的证书做出"处理"
        completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
    }
 }

@end

相关文章

  • Https访问

    有的时候Https访问我们可以直接拿到数据!!因为有的公司财大气粗!哥么有钱!!可以认证!!!认证过的Https访...

  • charles访问https

    安装步骤: 先安装charles中的证书: 提示导入成功就代表安装成功: 在手机上安装证书: 把手机的代理设置成1...

  • restTemplate访问https

    序 本文简述一下怎么使用restTemplate来访问https。 maven 这里使用httpclient的fa...

  • 访问HTTPS网页

    跳过证书验证 注:以淘宝网站为例。

  • Https访问http

    1.加上 referrerpolicy="origin" 2.加上 meta 可以在相应的页面的 里加上这句代码,...

  • Https访问配置

    1. 生成服务器端私钥及密钥库: keytool -genkey -aliastestServer-validit...

  • 配置https访问

    0. 说明 使用 NetBeans 12.4 GlassFish Server 5.0 Java EE 8 另外,...

  • 代码可以访问https,postman 访问https 失败解决

    postman 报错信息: 解决方案: 把on改为off

  • Apache 配置 HTTPS访问

    将需要配置的项目移动到另一根目录下,作为https访问位置。 修改bitnami配置文件..\Bitnami\wa...

  • nginx支持https访问

    引子 为了提高web应用的安全性,现在基本上都需要支持https访问。在此记录一下自己在nginx下的配置...

网友评论

    本文标题:Https访问

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