允许使用AFNetworking的无效证书 [英] allow invalid certificates with AFNetworking
问题描述
我一直在使用以下代码
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.allowsInvalidSSLCertificate=YES;
现在我已将AFNetworking更改为2.0的最新版本。使用 AFHTTPRequestOperation operation.allowsInvalidSSLCertificate 不再有效。根据我使用的文件
Now i have changed AFNetworking to latest version that is 2.0. operation.allowsInvalidSSLCertificate is not working anymore with AFHTTPRequestOperation. As per documents i used
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;
我的请求代码是
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog (@"success: %@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", error.description);
}
];
[operation start];
[operation waitUntilFinished];
但这不适用于需要证书的HTTPS。我该怎么做才能使这个工作?
But this is not working for HTTPS which require certificates. What should i do to make this work?
推荐答案
我通过在 [操作开始]之前添加以下代码解决了这个问题;
AFSecurityPolicy *sec=[[AFSecurityPolicy alloc] init];
[sec setAllowInvalidCertificates:YES];
operation.securityPolicy=sec;
这是因为 AFHTTPRequestOperationManager 未连接到 AFHTTPRequestOperation 。因此设置管理器的安全证书不能在requestOperation上做魔术。因此必须初始化并将其分配给 AFHTTPRequestOperation 。
This happened because AFHTTPRequestOperationManager is not connected to AFHTTPRequestOperation. So setting manager's security certificate can not do magic at requestOperation. So have to initialize and assign one to AFHTTPRequestOperation.
希望这有助于某人:)
这篇关于允许使用AFNetworking的无效证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!