使用 NSURLConnection 忽略证书错误

编程入门 行业动态 更新时间:2024-10-22 11:42:57
本文介绍了使用 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:51:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:证书   错误   NSURLConnection

发布评论

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

>www.elefans.com

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