码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-05-15 23:42:56      阅读:688      评论:0      收藏:0      [点我收藏+]

标签:https   uiwebview   

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


下面是我加载https站点的时候出现的错误。

error: 

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


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

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

HTTPS的信任继承基于预先安装在浏览器中的证书颁发机构(如VeriSign、Microsoft等)(意即“我信任证书颁发机构告诉我应该信任的”)。因此,一个到某网站的HTTPS连接可被信任,如果服务器搭建自己的https 也就是说采用自认证的方式来建立https信道,这样一般在客户端是不被信任的,所以我们一般在浏览器访问一些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




iOS UIWebView 加载https站点出现NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL,,布布扣,bubuko.com

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

标签:https   uiwebview   

原文地址:http://blog.csdn.net/shouqiangwei/article/details/25897941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!