错误
在应用中由 http转为 https 时, 报错了, 下面是错误:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)或者
Error Domain=NSURLErrorDomain Code=-1202 “The certificate for this server is invalid. “
原因
这是由于你的 https的证书失效, 或者是自建证书, 你需要跳过验证, 允许其进行服务器的连接.
请求错误中的解决方案
1.一般的,如果用的 AFN 的话你可以加上以下代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//allowInvalidCertificates 是否允许无效证书(也就是自建的证书),默认为NO
//如果是需要验证自建证书,需要设置为YES
securityPolicy.allowInvalidCertificates = YES;
//validatesDomainName 是否需要验证域名,默认为YES;
//假如证书的域名与你请求的域名不一致,需把该项设置为NO
//主要用于这种情况:客户端请求的是子域名,而证书上的是另外一个域名。因为SSL证书上的域名是独立的,假如证书上注册的域名是www.google.com,那么mail.google.com是无法验证通过的;当然,有钱可以注册通配符的域名*.google.com,但这个还是比较贵的。
securityPolicy.validatesDomainName = NO;
//validatesCertificateChain 是否验证整个证书链,默认为YES
//设置为YES,会将服务器返回的Trust Object上的证书链与本地导入的证书进行对比,这就意味着,假如你的证书链是这样的:
//GeoTrust Global CA
//
Google Internet Authority G2
//
*.google.com
//那么,除了导入*.google.com之外,还需要导入证书链上所有的CA证书(GeoTrust Global CA, Google Internet Authority G2);
//如是自建证书的时候,可以设置为YES,增强安全性;假如是信任的CA所签发的证书,则建议关闭该验证;
securityPolicy.validatesCertificateChain = NO;
requestOperationManager.securityPolicy = securityPolicy;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
2.若还是不可进行请求, 你可以尝试在你的请求类中加入以下代码:
复制代码
1
2
3
4
5
6
7
8
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler{
NSLog(@"didReceiveChallenge");
//
if([challenge.protectionSpace.host isEqualToString:@"api.lz517.me"] /*check if this is host you trust: */ ){
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
//
}
}
- 1
- 2
- 3
- 4
- 5
- 6
3.加入代码后本人遇到的问题就解决了, 但是如果你的请求还有问题你可以参考以下,
和这个问题 http://stackoverflow.com/questions/33827351/how-to-solve-this-nsurlsession-nsurlconnection-http-load-failed-kcfstreamerrord
这里也重点推荐一篇文章是关于 ATS 的: App Transport Security(ATS)
https自建证书在 AFN中的设置
网络请求及各类错误代码含义总结
UIWebView中解决方案实例:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145#import "LoginProTocalVC.h" @interface LoginProTocalVC (){ NSURLConnection *_urlConnection; NSURLRequest *_request; BOOL _authenticated; UIWebView *webV; } @end @implementation LoginProTocalVC - (void)viewDidLoad { [super viewDidLoad]; [self configNavigationBar]; [self configBody]; // Do any additional setup after loading the view. } - (void)configBody{ webV = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)]; [self.view addSubview:webV]; // if (currentType == YBNetworkTest) { // [view loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://services.xxx.com/newsdetail/4"]]]; // }else if(currentType == YBNetworkT1Service){ // [view loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://t1services.xx.com/newsdetail/4"]]]; // }else if (currentType == YBNetworkDistribution){ // [view loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://appservices.xxx.com/newsdetail/4"]]]; // }else if (currentType == YBNetworkApp1Service){ // [view loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://app1services.xxx.com/newsdetail/4"]]]; // } NSString *urlStr = @""; if (currentType == YBNetworkTest) { urlStr = @"https://www.xxx.com/newsdetail/4"; }else if(currentType == YBNetworkT1Service){ urlStr = @"https://www.xxx.com/newsdetail/4"; }else if (currentType == YBNetworkDistribution){ urlStr = @"https://www.xxx.com/newsdetail/4"; }else if (currentType == YBNetworkApp1Service){ urlStr = @"https://www.xxx.com/newsdetail/4"; } NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]]; webV.delegate = self; [webV loadRequest:request]; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"Did start loading: %@ auth:%d", [[request URL]absoluteString],_authenticated); if (!_authenticated) { _authenticated = NO; _request = request; _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self]; [_urlConnection start]; return NO; } return YES; } - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSLog(@"WebController Got auth challange via NSURLConnection"); if ([challenge previousFailureCount] == 0) { _authenticated = YES; NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; }else{ [[challenge sender] cancelAuthenticationChallenge:challenge]; } } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"WebController received response via NSURLConnection"); // remake a webview call now that authentication has passed ok. _authenticated = YES; [webV loadRequest:_request]; // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!) [_urlConnection cancel]; } -(void)configNavigationBar{ //创建颜色背景View UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)]; bgView.backgroundColor = [UIColor whiteColor]; // bgView.backgroundColor = [UIColor lightGrayColor]; bgView.clipsToBounds = YES; [self.view addSubview:bgView]; //创建返回按钮 UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; backBtn.frame = CGRectMake(5 , 20, 40, 40); [backBtn setImage:[UIImage imageNamed:@"regist_vc1_nv_back"] forState:UIControlStateNormal]; [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside]; [bgView addSubview:backBtn]; UILabel *titltlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, bgView.frame.size.width, 40)]; titltlabel.textAlignment = NSTextAlignmentCenter; titltlabel.textColor = [UIColor blackColor]; titltlabel.text = @"用户协议"; // titltlabel.font = [UIFont systemFontOfSize:15]; titltlabel.font = kYBTitleFont; [bgView addSubview:titltlabel]; UIImageView *lingImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, bgView.frame.size.height-0.5, bgView.frame.size.width, 0.5)]; lingImg.backgroundColor = [UIColor lightGrayColor]; [bgView addSubview:lingImg]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (void)backBtnClick { //zyp NSArray *viewcontrollers=self.navigationController.viewControllers; if (viewcontrollers.count>1) { if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) { //push方式 [self.navigationController popViewControllerAnimated:YES]; } } else{ //present方式 [self dismissViewControllerAnimated:YES completion:nil]; } //end } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
最后
以上就是虚拟戒指最近收集整理的关于iOS webview无法加载网页Domain=NSURLErrorDomain Code=-1202错误的全部内容,更多相关iOS内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复