网络:NSURLConnection 使用 HTTPS

来源:互联网 发布:淘宝手机端店招素材 编辑:程序博客网 时间:2024/05/21 19:23
#import "ViewController.h"@interface ViewController ()<NSURLConnectionDelegate,NSURLConnectionDataDelegate>@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    NSURL *url = [NSURL URLWithString:@"https://mail.itcast.cn"];    // 创建请求    NSURLRequest *request = [NSURLRequest requestWithURL:url];    [NSURLConnection connectionWithRequest:request delegate:self];}// 使用第三方另说// 信任HTTPS证书- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {    NSLog(@"%@",challenge.protectionSpace);    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {        // 信任证书        NSURLCredential *cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];        [challenge.sender useCredential:cre forAuthenticationChallenge:challenge];    }}// 接收到数据- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {    NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);}@end
0 0
原创粉丝点击