iOS 绕过https证书验证 请求数据

时间:2023-03-09 12:45:20
iOS 绕过https证书验证 请求数据
HTTPS和HTTP:
1、https协议需要到ca申请证书,一般免费证书很少,需要交费。
2、http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议。
3、http和https使用的是完全不同的连接方式,用的端口也不一样
4、http的连接很简单,是无状态的;HTTPS协议是由SSL HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。

因为项目需要调试,公司自己弄了个证书验证,所有需要在请求数据的时候绕过验证,在网上搜了下,最后代码贴上

- (void)session{

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:Nil];
NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://114.55.231.139/"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// NSLog(@"error:%@",error);
NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume]; }
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
if([challenge.protectionSpace.host isEqualToString:@"114.55.231.139"]){
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
} else {
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}
}
}

有说AFNetworking 不需要处理,本身就处理了,查了下貌似是的:
iOS 绕过https证书验证 请求数据