iOS 的三种自建证书方法https请求相关配置

本文详细介绍了在iOS应用中配置自建证书进行HTTPS请求的三种方法,包括AFNetworking、NSURLSession和NSURLConnection的实现,强调了安全策略的设置和证书验证的关键步骤。并提到了Apple对SSL证书的要求,建议采用本地导入证书的方式来确保安全性。
摘要由CSDN通过智能技术生成

架构师

在这里插入图片描述
失效联系981163085

会员链接

券链接

定期更新

0623更新链接

如果你的app服务端安装的是SLL颁发的CA,可以使用系统方法直接实现信任SSL证书,关于Apple对SSL证书的要求请参考:苹果官方文档CertKeyTrustProgGuide

这种方式不需要在Bundle中引入CA文件,可以交给系统去判断服务器端的证书是不是SSL证书,验证过程也不需要我们去具体实现。

第1种

#import “ViewController.h”

#import “HttpManager.h”

@interface ViewController ()

@property(nonatomic,copy)NSString *etag;

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

    [btn setTitle:@“AFNetworking” forState:UIControlStateNormal];

    [btn setBackgroundColor:[UIColor redColor]];

    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    UIButton *sessionBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 200, 100)];

    [sessionBtn setTitle:@“NSUrlSession” forState:UIControlStateNormal];

    [sessionBtn setBackgroundColor:[UIColor redColor]];

    [sessionBtn addTarget:self action:@selector(sessionBtnClicked) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:sessionBtn];

}

  • (void)btnClicked {

    NSString *urlString = @“https://10.20.129.25:8443/dreamVideo/restful/show”;

    HttpManager *httpManager = [HttpManager shareHttpManager];

    [httpManager post:urlString withParameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {

      NSLog(@"success");
    

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

      NSLog(@"failure");
    

    }];

}

  • (void)sessionBtnClicked {

    NSString *urlString = @“https://10.20.129.25:8443/dreamVideo/restful/show”;

    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request];

    [task resume];

}

  • (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask

didReceiveResponse:(NSURLResponse *)response

completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {

NSLog(@"接收到服务器响应");

//注意:这里需要使用completionHandler回调告诉系统应该如何处理服务器返回的数据

//默认是取消

/**

 NSURLSessionResponseCancel = 0,            默认的处理方式,取消

 NSURLSessionResponseAllow = 1,             接收服务器返回的数据

 NSURLSessionResponseBecomeDownload = 2,    变成一个下载请求

 NSURLSessionResponseBecomeStream           变成一个流

 */

completionHandler(NSURLSessionResponseAllow);

}

  • (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask

    didReceiveData:(NSData *)data {

    NSLog(@“获取到服务段数据”);

    NSLog(@“%@”,[self jsonToDictionary:data]);

}

  • (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task

didC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值