使用AFNetworking 1.x进行SSL固定

时间:2014-01-17 14:14:20

标签: ios objective-c ssl ssl-certificate afnetworking

我是AFNetworking框架和SSL Pinning的新手。 我已经做了:

#define _AFNETWORKING_PIN_SSL_CERTIFICATES_ = 1

但不要认为它足够,它是正确的吗? 我怎样才能做到这一点? 继承了我目前的要求:

NSArray *info = @{@"action" : @"test"}; 
NSError * error = nil; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest*request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:url]]; 
NSMutableData *body = [NSMutableData data]; 
[body appendData:jsonData]; 
[request setHTTPBody:body]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:request.URL];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation
*operation, id responseObject) {
    NSError *error;

    NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"response String %@",responseString); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error)  {
    NSLog(@"Error: %@",error.localizedDescription); }]; 
[operation start];

请求工作得很好,但我不知道它是否真的固定了ssl。

1 个答案:

答案 0 :(得分:0)

在致SSLPinningMode开始之前,您必须将AFSSLPinningModeCertificate媒体资源设为AFJSONRequestOperation