使用 Mac OS X 钥匙串上安装的自签名证书发送同步请求
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,你不能通过同步请求来做到这一点。至少对于 NSURLConnection 来说不是。您可以使用 ASIHTTPRequest ,它看起来像这样
: 3rd party libs,AFNetworking 也可以做到这一点(以多种方式),而且它也有一个选项同步工作。
最后但并非最不重要的一点是,您可以通过低级别并使用 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:
When speaking of 3rd party libs, AFNetworking can do this as well (in several ways) and it also has an option to work synchronously.
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.