一、问题场景

在混合开发应用中出现https资源无法访问的现象

 

二、问题分析

对于https的页面访问,之前测试过新浪https://www.sina.com.cn/ 能够正常访问,觉得应该是没有问题的,于是使用问题页面进行测试,

发现访问https://192.168.13.34 类似这样的IP形式的页面确实有问题。

安卓报错如下:

I/X509Util: Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

 

 

苹果报错如下:

#####didFailProvisionalNavigation error is Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “192.168.65.34” which could put your confidential information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _WKRecoveryAttempterErrorKey=<WKReloadFrameErrorRecoveryAttempter: 0x283e88440>, networkTaskDescription=LocalDataTask <D0FB2EA2-C3F5-403B-ACD5-4C8B75E799E3>.<2>, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9807, NSErrorPeerCertificateChainKey=(

"<cert(0x103710670) s: >"

),

 

根据报错来看,应该是这种非域名部署的https的方式没有信任证书导致,在网上找到的解决方案大体都是要处理ssl 协议部分的内容,具体如下:

https://www.jb51.net/article/128572.htm

 

重载如下回调

安卓

@override

public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){

//handler.cancel(); 默认的处理方式,WebView变成空白页

 handler.process();接受证书

//handleMessage(Message msg); 其他处理

}

 

苹果

// Code=-1202 "The certificate for this server is invalid

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

        NSURLCredential * card = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];

        completionHandler(NSURLSessionAuthChallengeUseCredential,card);

    }

}

 

 

 

测试访问页面

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
    
    
<div>
<a href="https://www.sina.com.cn/">新浪https</a>
</div>
<br>
<div>
<a href="https://192.168.65.41:8443/packonline/test.jpg">内部IP https</a>
</div>

</body>
</html>

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐