我需要通过https
protcol从我的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);
}
仅低于响应,
错误域= NSURLErrorDomain代码= -1012“操作无法完成。(NSURLErrorDomain错误-1012。)”UserInfo = 0x7fd268560900 {NSErrorFailingURLKey = https://myapi.com/Create,NSErrorFailingURLStringKey = https://myapi.com/Create}
我找到了github fixes issue但是,AFNetworking 2.x默认为(instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode;
。我怎么能把它整合到我的约束中呢。
有人对此有所了解吗?
答案 0 :(得分:2)
最后,我自己找到了答案。下面的代码解决了我的问题,
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; //不推荐用于制作
非常感谢