HTTPS request processing

HTTPS requests when:

[1] certificate is trusted, do nothing

[2] certificate is not trusted, is self-signed

  (1) modify the configuration file, disable ATS characteristics

  (2) install a digital certificate and trust

 

NSURLSession sample code is as follows:

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDataDelegate>

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSURL *url = [NSURL URLWithString:@"https://kyfw.12306.cn/otn/"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    [[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }] resume];
}


- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    NSLog(@"%@", challenge.protectionSpace);
    /*
    NSURLSessionAuthChallengeUseCredential = 0,            Use the specified credential, which may be nil
    NSURLSessionAuthChallengePerformDefaultHandling = 1,   Default handling for the challenge - as if this delegate were not implemented; the credential parameter is ignored.
    NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2, The entire request will be canceled; the credential parameter is ignored.
    NSURLSessionAuthChallengeRejectProtectionSpace = 3,         This challenge is rejected and the next authentication protection space should be tried; the credential parameter is ignored.
    */
    NSURLCredential *credentail = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential, credentail);
}

 

 

AFN settings

 

 

Guess you like

Origin www.cnblogs.com/lyz0925/p/11613746.html