NSURLConnection 实现webView显示HTTPS页面
来源:互联网 发布:红包自动抢软件 编辑:程序博客网 时间:2024/05/22 09:07
我们在浏览器访问https页面的时候的,会弹出:
我们接下来信任证书以及显示出来
遵循协议
@interface ViewController ()<NSURLConnectionDataDelegate>
interface:
@interface ViewController ()<NSURLConnectionDataDelegate>/** - 存储data数据 */@property(nonatomic,strong)NSMutableData *dataM;/** - 访问url链接 */@property(nonatomic,strong)NSURL *url;@property(nonatomic,weak)IBOutlet UIWebView *webView;@end
viewDidLoad:创建url以及发送请求
- (void)viewDidLoad { [super viewDidLoad]; self.url = [NSURL URLWithString:@"https://mail.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:self.url]; //发送请求 [NSURLConnection connectionWithRequest:request delegate:self];}
实现代理方法:
#pragma mark - NSURLSessionDataDelegate代理方法- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)taskdidReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler{ NSLog(@"challenge = %@",challenge.protectionSpace.serverTrust); //判断是否是信任服务器证书 if (challenge.protectionSpace.authenticationMethod ==NSURLAuthenticationMethodServerTrust) { //创建一个凭据对象 NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; //告诉服务器客户端信任证书 [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; }}/** * 接收到服务器返回的数据时调用 */- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"接收到的数据%zd",data.length); [self.dataM appendData:data];}/** * 请求完毕 */- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSString *html = [[NSString alloc]initWithData:self.dataM encoding:NSUTF8StringEncoding]; NSLog(@"请求完毕"); [self.webView loadHTMLString:html baseURL:self.url];}
懒加载:
- (NSMutableData *)dataM{ if (_dataM == nil) { _dataM = [[NSMutableData alloc]init]; } return _dataM;}
至此,我们已经实现了https数据的展示:
注意:
- NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843) 原因:没有信任服务器证书
在下面这个方法中:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)taskdidReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler()
我们通过protectionSpace.authenticationMethod判断是否信任服务器证书
- NSURLSessionAuthChallengeUseCredential = 0, 使用凭据 ,信任服务器证书
- NSURLSessionAuthChallengePerformDefaultHandling = 1, 默认处理,忽略服务器证书
- NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2, 整个请求被取消 凭据被忽略
- NSURLSessionAuthChallengeRejectProtectionSpace = 3, 本次拒绝,下次重试
3 0
- NSURLConnection 实现webView显示HTTPS页面
- 【Android】WebView加载https页面不能正常显示资源问题
- 【Android】WebView加载https页面不能正常显示资源问题
- Android WebView加载https页面不能正常显示资源问题
- Android 7.0系统webview 显示https页面空白处理
- webview加载https页面图片不能显示问题
- WebView加载https页面不能正常显示资源问题
- samsung 手机webview 加载https 页面不显示问题
- NSURLConnection实现HTTPS(SSL)链接请求
- webview显示https协议内容
- 使用WebView显示页面
- WebView页面显示不全
- android使用WebView实现显示360°全景H5页面
- android webview 访问https页面 SslError 处理
- 解决android webview 中打不开https页面
- android webview 访问https页面 SslError 处理
- android webview 访问https页面 SslError 处理
- webview加载https页面空白问题
- Android注释规范
- Exercises 3.17 输入一段文字到string类型的vector中,并将其改为大写
- 利用php+curl调用极光IM第三方REST API方法经验
- centos6.5使用JMX时远程连接不上的问题
- LeetCode 37 Sudoku Solver(求解数独)(*)
- NSURLConnection 实现webView显示HTTPS页面
- 提示Android APP性能
- c++编程必备啊..
- iOS 9开发(适配)中需要注意的事项总结
- 成为一名更好的程序员:如何阅读源代码
- android 显示flash视频播放器
- Larry Smithr在TED演讲:为什么你干不成一番大事业
- 本地json文件的编辑器,node-webkit开发的exe程序
- 诗词—《泯稚》