我试图使用AFNetworking 2,但失败了。
根据这些建议,我做了以下工作:
#import "AFHTTPSessionManager.h"
@interface OperationManager : AFHTTPRequestOperationManager
+ (instancetype)sharedClient;
@end
@implementation OperationManager
+ (instancetype)sharedClient {
static OperationManager *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[OperationManager alloc] initWithBaseURL:[NSURL URLWithString:kBaseURL]];
[_sharedClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]];
});
return _sharedClient;
}
@end下面是我尝试使用它的方法:
OperationManager * manager = [OperationManager sharedClient];
NSString * link = kAuth;
// AFHTTPRequestOperationManager* manager =[AFHTTPRequestOperationManager manager];
// NSString * link = [kBaseURL stringByAppendingString:kAuth];
[manager GET:link parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];常量来自示例项目:
static NSString * const kBaseURL = @"https://alpha-api.app.net/";
static NSString * const kAuth = @"stream/0/posts/stream/global";当我运行这段代码时,我得到的是:
错误:错误Domain=NSURLErrorDomain代码=-1012“操作无法完成。(NSURLErrorDomain错误-1012。)UserInfo=0x15d97e50 {NSErrorFailingURLKey=https://alpha-api.app.net/stream/0/posts/stream/global,NSErrorFailingURLStringKey=https://alpha-api.app.net/stream/0/posts/stream/global}
如果我用OperationManager和它的链接注释这2行,并取消用它的链接注释AFHTTPRequestOperationManager,那么一切都正常。
从昨天早上开始,我就在调试和挖掘答案。
发布于 2013-11-11 02:19:18
据我所见,AFHTTPRequestOperationManager具有默认的AFSecurityPolicy of AFSSLPinningModeNone。
在我看来,这是这两个电话的唯一区别。
https://stackoverflow.com/questions/19833413
复制相似问题