使用 Mac OS X 钥匙串上安装的自签名证书发送同步请求

发布于 2024-12-21 06:34:13 字数 481 浏览 2 评论 0原文

我正在使用 Mac OS X(不是 iphone)同步向 Web 服务器发送 https 请求,

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response   error:&error];

但是我收到错误代码=-1202 - “不受信任的服务器证书”

我有一个来自安装在 Mac 钥匙串上的服务器的自签名证书(并验证来自浏览器的 https 请求运行正常)。

我不想通过异步发送数据并处理 didReceiveAuthenticationChallenge 来忽略证书。

如果证书安装在钥匙串中,sendSynchronousRequest 应该不起作用。我错过了什么吗?

I'm sending a https request from Mac OS X (not iphone) to web server synchronously using

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response   error:&error];

However I'm getting error Code=-1202 - "untrusted server certificate"

I have a self signed certificate from the server which I installed on my Mac's keychain (and verified that https requests from browser are going fine).

I don't want to ignore the certificate by sending data asynchronously and handling didReceiveAuthenticationChallenge

Should sendSynchronousRequest not work if the certificate is installed in keychain. Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑咖 2024-12-28 06:34:13

据我所知,你不能通过同步请求来做到这一点。至少对于 NSURLConnection 来说不是。您可以使用 ASIHTTPRequest ,它看起来像这样

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setValidatesSecureCertificate:NO]
[request startSynchronous];

: 3rd party libs,AFNetworking 也可以做到这一点(以多种方式),而且它也有一个选项同步工作。

#ifdef DEBUG
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_     
#endif

最后但并非最不重要的一点是,您可以通过低级别并使用 CFNetworking 本身来做到这一点(您可以查看 ASIHTTPRequest 代码如何执行此操作),但这可能会比使用 NSURLConnectionDelegate 添加更多的样板代码。

我还提到了第 3 方库,因为它们使用不同的方法来异步 HTTP 请求。我鼓励你看看它们,因为它们可能更适合你不想使用 NSURLConnectionDelegate 的原因。

As far as I know you can't do this with a synchronous request. At least not with NSURLConnection. You could use ASIHTTPRequest and it would look like this:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setValidatesSecureCertificate:NO]
[request startSynchronous];

When speaking of 3rd party libs, AFNetworking can do this as well (in several ways) and it also has an option to work synchronously.

#ifdef DEBUG
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_     
#endif

Last but not least you can do it whith going low level and using CFNetworking itself (you can look at the ASIHTTPRequest code how to do this), but this would probably add more boilerplate code than using NSURLConnectionDelegate.

I also mentioned the 3rd party libs because they use different approaches to asynchronise HTTP requests. I encourage you to have a look at them, because they may better fit the reasons why you don't want to use NSURLConnectionDelegate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文