【问题标题】:allow invalid certificates with AFNetworking允许使用 AFNetworking 的无效证书
【发布时间】:2013-12-28 12:53:48
【问题描述】:

我一直在使用以下代码

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.allowsInvalidSSLCertificate=YES;

现在我已将 AFNetworking 更改为 2.0 的最新版本。 operation.allowsInvalidSSLCertificate 不再适用于 AFHTTPRequestOperation。根据我使用的文件

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。我应该怎么做才能完成这项工作?

【问题讨论】:

    标签: ios objective-c afnetworking-2


    【解决方案1】:

    我通过在[操作开始]之前添加以下代码解决了这个问题;

    AFSecurityPolicy *sec=[[AFSecurityPolicy alloc] init];
    [sec setAllowInvalidCertificates:YES];
    operation.securityPolicy=sec;
    

    发生这种情况是因为 AFHTTPRequestOperationManager 未连接到 AFHTTPRequestOperation。所以设置管理器的安全证书不能在 requestOperation 中发挥作用。所以必须初始化并分配一个给AFHTTPRequestOperation

    希望这对某人有所帮助:)

    【讨论】:

    • 我确认这是解决方案。但是,我希望 AFNetworking 提供更明确的错误消息,而不是“取消”,这并不意味着什么。
    【解决方案2】:

    据我所知。这是 iOS 7 的问题。 Apple 不允许与自签名证书网站进行通信。除非您将证书发送到设备并添加为受信任的证书列表。

    对其他问题的支持评论:https://stackoverflow.com/a/20251011/753603

    找不到 Apple 记录的文本。

    【讨论】:

    猜你喜欢
    • 2016-01-18
    • 2014-09-17
    • 2017-05-27
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多