AFNetworking SSL证书失败iOS

时间:2015-08-03 15:33:52

标签: ios ssl ssl-certificate afnetworking-2

我花了很多时间寻找答案,但仍然没有运气,所以我决定放弃并寻求社区帮助:

1我有一个带有自签名证书的开发服务器

2是的,我确实使用了policy.allowInvalidCertificates = YES;

3 SSL位于自定义端口44302上

4我需要运行下载任务,因为请求返回数据流

这是我的代码:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
manager.securityPolicy = policy;

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"https://self_signed_cert.net:44302/storage/%@/action.download", _ticketId]];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];
[dataTask resume];

这是错误:

  

2015-08-03 18:18:15.901 SSL_TEST [1517:23943]错误:错误   Domain = NSURLErrorDomain Code = -1200"发生SSL错误并且a   无法与服务器建立安全连接。"的UserInfo = 0x787d2f20   {NSLocalizedDescription =发生了SSL错误并且是安全的   无法连接到服务器。,   NSLocalizedRecoverySuggestion =您想要连接到服务器吗?   无论如何?,_ kCFStreamErrorCodeKey = -9806,   NSErrorFailingURLStringKey = https://self_signed_cert.net:44302/storage/a430f316cfe076e9850874c0edad3dcb/action.download,   _kCFStreamErrorDomainKey = 3,NSUnderlyingError = 0x787da700"操作无法完成。 (kCFErrorDomainCFNetwork错误   -1200。)",NSErrorFailingURLKey = https://self_signed_cert.net:44302/storage/a430f316cfe076e9850874c0edad3dcb/action.download}

非常感谢您的帮助!感谢。

1 个答案:

答案 0 :(得分:0)

我尝试过一个空项目,它适用于App:

NSURL* lBaseURL = [NSURL URLWithString:@"https://myDomain/"];
    AFHTTPSessionManager* lSessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:lBaseURL
                                              sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];


    AFSecurityPolicy* lPolicy= [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    lPolicy.allowInvalidCertificates = YES;

    lSessionManager.securityPolicy = lPolicy;


    [lSessionManager GET:@"https://myDomain/api/"
              parameters:nil
                 success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull JSON)
     {
         NSLog(@"success %@", JSON);
     }
                 failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error)
     {
         NSLog(@"failure %@", error);
     }];
  • 不要忘记将 .cer 文件
  • 添加到您的目标(在应用和观看应用中)
  • 将ATS激活到 .plist NSAppTransportSecurity

但是我坚持使用watchOS 2.0扩展。它只是失败了这个错误:

  

错误域= NSURLErrorDomain代码= -1200“发生了SSL错误   并且无法与服务器建立安全连接。“   UserInfo = {NSLocalizedRecoverySuggestion =您要连接到   无论如何服务器?,NSErrorFailingURLKey = https://myDomain/api,   _kCFStreamErrorCodeKey = -9802,NSErrorFailingURLStringKey = https://myDomain/api,   NSErrorClientCertificateStateKey = 0,_kCFStreamErrorDomainKey = 3,   NSLocalizedDescription =发生了SSL错误并且是安全的   无法连接到服务器。}