1

I am trying to use AFNetworking 2, and I fail.

Following the recommendations, I did the following:

#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

And here is how I am trying to use it:

    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);
    }];

the constants are used from example project:

static NSString * const kBaseURL = @"https://alpha-api.app.net/";
static NSString * const  kAuth = @"stream/0/posts/stream/global";

When i run this code, here is what I get:

Error: Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -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}

If I comment the 2 lines with OperationManager and it's link, and uncomment the AFHTTPRequestOperationManager with it's link, everything works.

I am debugging and digging for a answer since yesterday's morning.

4

1 回答 1

0

As far as I can see, the AFHTTPRequestOperationManager has a default AFSecurityPolicy of AFSSLPinningModeNone.

That appears to me to be the only difference between the two calls.

于 2013-11-11T02:19:18.303 回答