如何在uiwebview中调用https url?

 brucegogo03 发布于 2023-02-13 08:05

我有一个HTTPS网址.它正在iPhone Safari中加载,但无效UIWebView.

错误是:

NSURLConnection/CFURLConnection HTTP加载失败(kCFStreamErrorDomainSSL,-9813)

我该如何解决这个问题?

1 个回答
  • 我已在下面解释了如何在UIWebview中访问https网址,它将帮助您清除问题,因为它适用于我.

    呼叫http与https url相同.

    但是,如果您使用的是自签名证书,则需要添加一些其他代码.因为默认情况下iOS会拒绝所有不受信任的https连接.

    限制不受信任的连接是非常好的默认行为,任何禁用此操作都是非常危险的.所以我们将显示警报,因为我们将绕过默认行为.

    -(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;
    
    -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
    

    上述两种方法允许我们为可信连接提供自己的身份验证机制

    #import "ClassCon.h"
    //  For now, I've hard coded the IP address of my trusted server.
    #define TRUSTED_HOST @"192.168.1.2"
    
    
    @implementation ClassCon {
        NSMutableData *contentData;
        NSURLConnection *conn;
    }
    
    -(void) loadContent {
        contentData = [NSMutableData data];
        NSString *contentURL = @"our url";
        conn = [[NSURLConnection alloc] initWithRequest:
                [NSURLRequest requestWithURL:[NSURL URLWithString:contentURL]] delegate:self];
    
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [contentData appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        NSLog(@"Bad: %@", [error description]);
        ContentResponse *response = [[ContentResponse alloc]initWithRc:-999 andDescr:@"error" andAction:0];
    
    
        conn = nil;
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
            NSString *loadedContent = [[NSString alloc] initWithData:
                                         contentData encoding:NSUTF8StringEncoding];
            NSLog(@"Loaded content: %@",loadedContent);
    
        }
    
    // ------------ ByPass ssl starts ----------
    -(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:
    (NSURLProtectionSpace *)protectionSpace {
        return [protectionSpace.authenticationMethod
                isEqualToString:NSURLAuthenticationMethodServerTrust];
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:
    (NSURLAuthenticationChallenge *)challenge {
        if (([challenge.protectionSpace.authenticationMethod
              isEqualToString:NSURLAuthenticationMethodServerTrust])) {
            if ([challenge.protectionSpace.host isEqualToString:TRUSTED_HOST]) {
                NSLog(@"Allowing bypass...");
                NSURLCredential *credential = [NSURLCredential credentialForTrust:
                                               challenge.protectionSpace.serverTrust];
                [challenge.sender useCredential:credential
                     forAuthenticationChallenge:challenge];
            }
        }
        [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
    }
    // -------------------ByPass ssl ends
    
    
    
    
    @end
    

    希望这可以帮助

    2023-02-13 08:10 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有