When iOS UIWebView loads an https site, NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL,

Source: Internet
Author: User

The following error occurs when loading an https site. Therefore, you have made some modifications to the embedded webview in iOS, so that it can load the http site or load the https site,


The following error occurs when I load the https site.

Error:

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


HTTPS Hypertext Transfer security Protocol (Abbreviation: 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 andWhen the server certificate can be verified and trustedProvides reasonable protection for eavesdropping and man-in-the-middle attacks.

HTTPS trust inheritance is based on the Certificate Authority (such as VeriSign and Microsoft) That is pre-installed in the browser (meaning "I trust the Certificate Authority to tell me what I should trust "). Therefore, an HTTPS connection to a website can be trusted. If the server builds its own https, that is, it uses the self-authenticated method to establish an https channel, which is generally untrusted on the client, therefore, we usually have a prompt when visiting some https sites in the browser to ask if you want to continue.

This also happens when you load an https site using webview. That is to say, you must set the site to secure during the request to continue accessing the site.

Therefore, when webview starts to load a webpage, we need to first determine whether the website is an https site. If yes, it will pause loading first.

NSURLConnection is used to access and change the site, and then set the site to a trusted site during authentication. Then, reload the request with webview.


# Pragma mark-UIWebViewDelegate-(BOOL) webView :( UIWebView *) awebView authorization :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {NSString * scheme = [[request URL] scheme]; NSLog (@ "scheme = % @", scheme); // determine whether it is https if ([scheme isEqualToString: HTTPS]) {// if it is https, then NSURLConnection is used to resend the request. In this case, the request URL is required for trust processing during the request process. 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 ;}


Handle the trust issue in the NSURLConnection proxy method.


-(Void) connection :( NSURLConnection *) connection willSendRequestForAuthenticationChallenge :( NSURLAuthenticationChallenge *) challenge {if ([challenge previusfailurecount] = 0) {_ authed = YES; // NSURLCredential indicates that the creden。 of identity authentication are unchangeable. The constructor of the class declared by the actual type of the credential to determine. NSURLCredential * Re = [NSURLCredential credentialForTrust: challenge. protectionSpace. serverTrust]; [challenge. sender useCredential: crecreauthenticationchallenge: challenge];} else

After receiving the response in the NSURLConnection proxy method, use web view to load the https site again.

Pragma mark ========================= NSURLConnectionDataDelegate
  
   
-(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 recommendedStackoverflow 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




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.