AFNetwork错误:错误域= NSURLErrorDomain代码= -1012

时间:2015-12-07 09:36:52

标签: objective-c afnetworking

我使用AFNetwork如下。 首先,我将安全策略设置如下:

  -(AFSecurityPolicy*)customSecurityPolicy:(BOOL)isHTTPS
  {
    if (isHTTPS) {
        NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"ehub-mobile" ofType:@".cer"];
        NSData *certData = [NSData dataWithContentsOfFile:cerPath];
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
        [securityPolicy setPinnedCertificates:@[certData]];
        [securityPolicy setValidatesDomainName:NO];
        [securityPolicy setAllowInvalidCertificates:YES];
        return securityPolicy;
    }
    else
    {
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
        return securityPolicy;
    }
  }

接下来是设置经理:

    -(void)UserLoginRequest:(NSString *)name Session:(NSString *)session
  {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.securityPolicy = [self customSecurityPolicy:YES];

    NSString *uName = name;
    NSString *uToken = session;
    NSDictionary *parameters = @{@"username": uName,@"token":uToken};
    ServerURLAddress *sua = [[ServerURLAddress alloc]init];
    NSString *server = [NSString stringWithFormat:@"%@/session",
                        [sua serverURLAddress]];
    server = [server stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager POST:server parameters:parameters
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              [self DisableSingleTap];
              NSString *lg_rep = operation.responseString;
              [self SeizeTheLoginInfo:lg_rep];
          }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"%@",error);
              [self EnableSingleTap];
          }];
  }

但它只是出现了这个错误,它发生在“失败”的块中,我使用NSLog函数来打印它:

Error Domain=NSURLErrorDomain Code=-1012 "(null)" 
UserInfo={NSErrorFailingURLKey=https://xxx.xxx.com

我读了很多文章,但我仍然找不到任何解决方案。 请给我一些提示!

1 个答案:

答案 0 :(得分:3)

试试这个,

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[securityPolicy setValidatesDomainName:NO];
[securityPolicy setAllowInvalidCertificates:YES];
manager.securityPolicy = securityPolicy;