zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS UIWebView 载入https 网站出现NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL,

iosFailedHTTPS网站HTTP 出现 load 载入
2023-09-14 09:08:01 时间

今天在载入https网站的时候遇到例如以下的错误问题。所以对自己之前写的iOS内嵌webview做了一些改动,能够让它载入http网站也能够让它载入https网站、


以下是我载入https网站的时候出现的错误。

error: 

   NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)


HTTPS 超文本传输安全协议(缩写:HTTPS,英语:Hypertext Transfer Protocol Secure)是超文本传输协议SSL/TLS的组合。

HTTPS的主要思想是在不安全的网络上创建一安全信道。并可在使用适当的加密包和server证书可被验证且可被信任时。对窃听中间人攻击提供合理的保护。

HTTPS的信任继承基于预先安装在浏览器中的证书颁发机构(如VeriSign、Microsoft等)(意即“我信任证书颁发机构告诉我应该信任的”)。

因此。一个到某站点的HTTPS连接可被信任,假设server搭建自己的https 也就是说採用自认证的方式来建立https信道,这样一般在client是不被信任的。所以我们一般在浏览器訪问一些https网站的时候会有一个提示,问你是否继续。

使用webview载入https网站的时候,也会出现这种情况。也就是说我们必须在请求的时候将该网站设置为安全的,才干继续訪问

所以我们须要在webview開始载入网页的时候首先推断推断该网站是不是https网站。假设是的话,先然他暂停载入,先用

NSURLConnection 来訪问改网站,然后再身份验证的时候,将该网站置为可信任网站。

然后在用webview又一次载入请求。


#pragma mark - UIWebViewDelegate

- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString* scheme = [[request URL] scheme];
    NSLog(@"scheme = %@",scheme);
    //推断是不是https
    if ([scheme isEqualToString:HTTPS]) {
         //假设是https:的话,那么就用NSURLConnection来重发请求。从而在请求的过程其中吧要请求的URL做信任处理。
        if (!self.isAuthed) {
            originRequest = request;
            NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
            [conn start];
            [awebView stopLoading];
            return NO;
        }
    }

    [self reflashButtonState];
    [self freshLoadingView:YES];

    NSURL *theUrl = [request URL];
    self.currenURL = theUrl;
    return YES;
}


在NSURLConnection 代理方法中处理信任问题。


- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

    if ([challenge previousFailureCount]== 0) {
        _authed = YES;

        //NSURLCredential 这个类是表示身份验证凭据不可变对象。凭证的实际类型声明的类的构造函数来确定。
        NSURLCredential* cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        [challenge.sender useCredential:cre forAuthenticationChallenge:challenge];
    }
    else<pre name="code" class="objc">


最后在NSURLConnection 代理方法中收到响应之后,再次使用web view载入https网站。

pragma mark ================= NSURLConnectionDataDelegate <NSURLConnectionDelegate>

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{

    NSLog(@"%@",request);
    return request;
    
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    self.authed = YES;
    //webview 又一次载入请求。
    [webView loadRequest:originRequest];
    [connection cancel];
}



推荐两个stackoverflow地址:

http://stackoverflow.com/questions/11573164/uiwebview-to-view-self-signed-websites-no-private-api-not-nsurlconnection-i


http://stackoverflow.com/questions/20365774/call-https-url-in-uiwebview


DEMO下载地址:https://github.com/wsq724439564/HTTPS--