zoukankan      html  css  js  c++  java
  • Has anybody found a way to load HTTPS pages with an invalid server certificate using UIWebView?

    If a user attempts to load a https web page in Mobile Safari and the server's certificate validation check fails (its expired, revoked, self-signed etc.) then the user is presented is presented with a warning message and asked if they want to continue or not.

    Similarly NSURLConnection offers the ability for the implementator to decide firstly how to check the certificate and then decide how to proceed if it fails, so in this situation too it would be possible to display a warning to the user and offer them the opportunity to continue loading the page or not.

    However it seems when loading a https page in UIWebView that fails a certificate check the behaviour is just to fail to load the page - didFailLoadWithError: gets called with kCFURLErrorServerCertificateUntrusted however nothing gets displayed to the user.

    This is inconsistent - surely the UIWebView behaviour should behave in a similar way to Safari to be consistent within iPhone itself? Its also a daft that NSURLConnection allows total flexibility with this yet NSURLRequest:setAllowsAnyHTTPSCertificate is private.

    Is there anyway to implement behaviour which is consistent with Safari, can this default behavior be customized in a similar way to NSURLConnection allows?

    Cheers

    P.S. Please refrain from getting into patronizing side discussions about why would anybody want to do this, thank you very much.

    I found out how to do this:

    1) When the page is loaded it will fail, thus add something like the following to didFailLoadWithError:

    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
        if ([error.domain isEqualToString: NSURLErrorDomain])
        {
            if (error.code == kCFURLErrorServerCertificateHasBadDate        ||
                error.code == kCFURLErrorServerCertificateUntrusted         ||
                error.code == kCFURLErrorServerCertificateHasUnknownRoot    ||
                error.code == kCFURLErrorServerCertificateNotYetValid)
            {
            display dialog to user telling them what happened and if they want to proceed

    2) If the user wants to load the page then you need to connect using an NSURLConnection:

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.currentURL     cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
    self.loadingUnvalidatedHTTPSPage = YES;
    [self.webView loadRequest:requestObj];

    3) Then make this change to shouldStartLoadWithRequest

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
    {
        if (self.loadingUnvalidatedHTTPSPage)
        {
            self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
            [self.connection start];
            return NO;
        }

    4) Implement the NSURLConnectionDelegate as:

    - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        SecTrustRef trust = challenge.protectionSpace.serverTrust;
        NSURLCredential *cred;
        cred = [NSURLCredential credentialForTrust:trust];
        [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
    }
    
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    {
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.currentURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
        self.loadingUnvalidatedHTTPSPage = NO;
        [self.webView loadRequest: requestObj];
        [self.connection cancel];
    }

    It all seems to work fine.

  • 相关阅读:
    大爽python系列教程 书单
    大爽Python入门教程 71 认识类`class`
    大爽Python入门教程 72 面向对象编程 使用类`class`组织代码
    大爽Python入门教程 75 异常处理 try ... except Exception
    大爽Python入门教程 73 面向对象编程 封装、继承、多态
    大爽Python入门教程 82 Python 库(Library)、包(Package)、模块(Module)
    大爽Python入门练习题 51 实践练习一 简单购物系统(控制台实现)
    大爽Python入门教程 81 导入`import`
    大爽Python入门教程 74 实践演示 控制台版本——简易回合战斗
    大爽Python入门练习题 52 实践练习二 推箱子游戏(控制台实现)
  • 原文地址:https://www.cnblogs.com/lisa090818/p/4276006.html
Copyright © 2011-2022 走看看