使用NSURLConnection忽略证书错误

编程入门 行业动态 更新时间:2024-10-22 16:28:39
本文介绍了使用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忽略证书错误

本文发布于:2023-11-07 22:50:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567653.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:证书   错误   NSURLConnection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!