我需要通过 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/