IOS UIWebView loading HTTPS site appears nsurlconnection/cfurlconnection HTTP load failed (KCFSTREAMERRORDOMAINSSL,

Source: Internet
Author: User

Today, when loading an HTTPS site, you encounter the following error. So I made some changes to the iOS embedded webview that I wrote before, so that it can load the HTTP site and let it load the HTTPS site,


Here are the errors that occurred when I loaded the HTTPS site.

Error

Nsurlconnection/cfurlconnection HTTP Load failed (Kcfstreamerrordomainssl,-9813)


HTTPS Hypertext Transfer Security Protocol (abbreviated: HTTPS, English: Hypertext Transfer Protocol Secure) is a combination of Hypertext Transfer Protocol and SSL/TLS.

The main idea of HTTPS is to create a secure channel on an insecure network and to provide reasonable protection against eavesdropping and inter-person attacks when the appropriate encryption packets and server certificates can be verified and trusted.

The trust inheritance for HTTPS is based on a certificate authority that is preinstalled in the browser (such as VeriSign, Microsoft, and so on) (meaning "I trust the certification authority to tell me that I should trust"). Therefore, an HTTPS connection to a website can be trusted, if the server set up its own HTTPS that is to use self-certification to establish an HTTPS channel, so generally in the client is not trusted, so we generally in the browser to visit some HTTPS site when there will be a hint, Ask if you continue.

When using WebView to load HTTPS sites, this situation also occurs, that is, we must set the site to be secure at the time of the request in order to continue to access

So we need to start loading the page at the beginning of the webview to determine whether the site is not HTTPS site, if so, before he paused loading, first with

Nsurlconnection the site to a trusted site when it accesses the site and then authenticates. Then reload the request with WebView.


 #pragma mark-uiwebviewdelegate-(BOOL) WebView: (UIWebView *) Awebview shouldstartloadwithrequest: (Nsurlrequest *)    Request Navigationtype: (uiwebviewnavigationtype) navigationtype{nsstring* scheme = [[Request URL] scheme];    NSLog (@ "scheme =%@", scheme); Determine if HTTPS if ([scheme Isequaltostring:https]) {//If it is https: then use Nsurlconnection to re-send the request.        So in the process of the request to request the URL to do the trust processing.            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;} 


The trust issue is handled in the Nsurlconnection proxy method.


-(void) connection: (Nsurlconnection *) connection Willsendrequestforauthenticationchallenge: ( Nsurlauthenticationchallenge *) challenge{    if ([Challenge previousfailurecount]== 0) {        _authed = YES;        Nsurlcredential This class is an immutable object that represents an authentication credential. The actual type of the credential declares the constructor of the class to determine.        nsurlcredential* cre = [nsurlcredential credentialForTrust:challenge.protectionSpace.serverTrust];        [Challenge.sender usecredential:cre forauthenticationchallenge:challenge];    }    Else<pre name= "code" class= "OBJC" >

Finally, after you receive the response in the Nsurlconnection proxy method, use Web view to load the HTTPS site again.

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 Reload Request.    [WebView loadrequest:originrequest];    [Connection Cancel];}



Two recommended StackOverflow address:

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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.