我正在尝试使用iOS 9+中的UIWebView
加载 https:// 网页
并且它给出了以下错误:
NSURLSession / NSURLConnection HTTP加载失败 (kCFStreamErrorDomainSSL,-9802)CFNetwork SSLHandshake失败(-9802)
我已经尝试了各种使用NSURLConnection
首先使用connection:willSendRequestForAuthenticationChallenge:
方法处理身份验证质询的方法。
虽然这适用于直接请求,但是 (此处实际问题已经开始)如果网页重定向到具有不同域的其他网页,则无效
例如,最初我通过调用 UIWebView
加载https://example.com
,然后此页面重定向到 https://someothersecuredomain.com
。这是连接失败并且未调用connection:willSendRequestForAuthenticationChallenge:
。
以下是我的实施:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Load webview with request url and parameters
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPBody:postBody];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
self.webView.delegate = self;
[self.webView loadRequest: request];
_BaseRequestURL = url;
}
#pragma mark UIWebView delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (![[request.URL absoluteString] isEqualToString:[_FailedRequest.URL absoluteString]]) {
_FailedRequest = request;
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
return NO;
}
return YES;
}
#pragma mark - NSURLConnectionDataDelegate methods
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURL *baseURL = [_FailedRequest URL];
if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) {
NSLog(@"trusting connection to host %@", challenge.protectionSpace.host);
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
} else
NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host);
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)pResponse {
[connection cancel];
[self.webView loadRequest:_FailedRequest];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"connection error : %@", error);
}
这是记录的连接错误:
错误域= NSURLErrorDomain代码= -1200“发生了SSL错误 并且无法与服务器建立安全连接。“ UserInfo = {NSUnderlyingError = 0x138643250 {错误 Domain = kCFErrorDomainCFNetwork Code = -1200“发生了SSL错误 并且无法与服务器建立安全连接。“ 的UserInfo = {NSErrorFailingURLStringKey = https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction, NSLocalizedRecoverySuggestion =您想要连接到服务器吗? 无论如何?,_kCFNetworkCFStreamSSLErrorOriginalValue = -9802, _kCFStreamPropertySSLClientCertificateState = 0,NSLocalizedDescription =发生了SSL错误并且安全 无法建立与服务器的连接。,_ kCFStreamErrorDomainKey = 3, NSErrorFailingURLKey = https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction, _kCFStreamErrorCodeKey = -9802}},NSErrorFailingURLStringKey = https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction, NSErrorFailingURLKey = https://secure.ccavenue.com/transaction/transaction.do?command=initiateTransaction, NSLocalizedRecoverySuggestion =您想要连接到服务器吗? 无论如何?,NSLocalizedDescription =发生了SSL错误并且是安全的 无法连接到服务器。}
根据iOS 9的应用传输安全性,我已将这两个域添加到我的Info.plist
中的例外域列表中。
这是这样的:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
<key>secure.ccavenue.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
我真的被困在这里,任何帮助都会受到赞赏。
PS。已经尝试过以下问题:Question 1,Question 2,Question 3,Question 4和Question 5