使用 NSURLConnection 忽略证书错误 [英] Ignoring certificate errors with NSURLConnection

查看:32
本文介绍了使用 NSURLConnection 忽略证书错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误

The certificate for this server is invalid. You might be connecting to a server
that is pretending to be "server addres goes here" which could put your
confidential information at risk."

我正在使用这种方法:

[NSURLConnection sendSynchronousRequest:request
                      returningResponse:&response
                                  error:&error];

我该如何解决这个问题?

How can I fix this?

我试过这个代码:

 NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                               delegate:self];

但是我在 didReceiveResponse 方法中得到了 EXC_BAD_ACCESS.

but then I get EXC_BAD_ACCESS in the didReceiveResponse method.

推荐答案

如果您不发送任何敏感信息,您可以简单地忽略无效证书.这篇文章介绍了如何做到这一点.以下是 Alexandre Colucci 的示例实现,用于在那篇文章.

You could simply ignore the invalid certificate if you are not sending any sensitive information. This article describes how you could do that. Here is an example implementation by Alexandre Colucci for one of the methods described in that article.

本质上,您想在 @implementation 上方定义一个虚拟接口:

Essentially you want to define a dummy interface just above the @implementation:

@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

在你调用 sendSynchronousRequest 之前,调用你在虚拟接口中定义的私有方法:

And before you call sendSynchronousRequest, invoke the private method you defined in the dummy interface:

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];

这篇关于使用 NSURLConnection 忽略证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆