ios - AFNetworking + HTTPS(未验证身份)

标签 ios objective-c https afnetworking-2

我需要通过 https 协议(protocol)从我的 Web 服务器获取记录。我的 API 调用具有基本身份验证。现在,我正在尝试使用 AFNetworking 2.0x 版

变通后找出添加基本身份验证的最简单方法 here现在,我遇到了 https 调用的问题。当我执行我的代码时,

NSDictionary *params = ....;

AFHTTPRequestOperationManager *manager = [SBAPIManager sharedManager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

@try {
    [manager POST:@"Create" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success - %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
} @catch (NSException *exception) {
    NSLog(@"Exception - %@", exception.description);
} 

我只得到低于响应,

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7fd268560900 {NSErrorFailingURLKey=https://myapi.com/Create, NSErrorFailingURLStringKey=https://myapi.com/Create}

我找到了 github fixes issue但是,AFNetworking 2.x默认情况下具有 (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode;。我如何将其整合到我的约束中。

有人对此有想法吗?

最佳答案

最后,我自己找到了答案。我已经用下面的代码解决了我的问题,

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:API_URL]];
manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

如果我们想将 AFNetworking 固定到 AFHTTPRequestOperationManager,我们必须添加以下一行,这将提供结果。

manager.securityPolicy.allowInvalidCertificates = YES; // not recommended for production

引用:AFNetworking

关于ios - AFNetworking + HTTPS(未验证身份),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26885930/

相关文章:

node.js - 将 HTTPS 服务器从 Express 迁移到 Hapi

amazon-web-services - 如果我没有域,我在哪里可以获得公共(public) SSL 证书?

ssl - Movable Type Administration to https 子域

javascript - 在移动 Safari 中检测 iOS5(首选 javascript)

ios - 突出显示 uicollectionviewcell 的正确方法

objective-c - 在 NSView 中加载 SWF

objective-c - 在 UIScrollView 中定位 UIView

java - 如何将 iOS NSLocale localeIdentifier 转换为 Java Locale?

ios - 如何将图像文字添加到数组并显示它们?

ios - 检测点击 MK ANNOTATION View 标注气泡