[英]HTTPS with NSURLConnection - NSURLErrorServerCertificateUntrusted
我有一個通過http連接的應用程序。 嘗試https時,我收到的錯誤表明根證書不受信任。 我找到了我的站點證書,其CA證書和CA的根證書的URL,並通過Safari將它們添加到手機中。 現在,當我轉到首選項 - >常規 - >配置文件時,我可以看到所有證書一直在鏈上。 每個證書上都有一個紅色的未簽名標簽。 當我連接時,我得到NSURLErrorServerCertificateUntrusted錯誤。 我想弄清楚接下來要去哪里。
任何幫助都會很棒。 唯一可能影響這一點的是我連接到一個奇數端口。 所以我的網址是www.domain.com:port。 端口號是否創建證書 - 域名不匹配?
現在我使用iPhone配置實用程序將配置配置文件添加到手機。 它有我的根證書,ca證書和站點證書。 手機上的個人資料顯示已經過驗證。 在細節中,我可以看到我的三個證書。 但是當我嘗試連接時,仍然會收到不受信任的證書錯誤。 有什么建議么?
試着看看是否還有其他人可以幫忙解決這個問題?
在NSURLConnection加載期間,有一個支持的API用於忽略錯誤的證書。 為此,只需將這樣的內容添加到NSURLConnection委托:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
if (... user allows connection despite bad certificate ...)
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
請注意,connection:didReceiveAuthenticationChallenge:如果需要,在向用戶顯示對話框之后,可以稍后將其消息發送到challenge.sender(等等)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.