iOS https访问如何绕过无效证书权限访问
一,由于在项目中遇到一个网页访问会涉及多个证书的情况,每一种证书都只且仅需要绕一次权限,但是我们无法判断什么时候更换了证书。所以,网上的方法总是不能完美的解决问题。以下方法可以通用遇到的各种情况,同时非常感谢网友closure为我提供的这种方法。
@property NSURLRequest *FailedRequest;
@property NSURLConnection *connection;
@property NSMutableSet *whiteList;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL =[request URL];
NSLog(@"this is web url : %@",requestURL);
if ( [ [ requestURL scheme ] isEqualToString: @"http" ])
{
NSString *requestString = [requestURL absoluteString];
if ([requestString hasSuffix:@"callback.html" ]) {
[self.navigationController popViewControllerAnimated:YES];
}
}
NSString* scheme = [[request URL] scheme];
if ([scheme isEqualToString:@"https"]) {
//如果是https:的话,那么就用NSURLConnection来重发请求。从而在请求的过程当中吧要请求的URL做信任处理。
BOOL result = [_whiteList containsObject:request];
if (!result) {
_FailedRequest = request;
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
return result;
}
return YES;
}
//要服务器端单项HTTPS 验证,iOS 客户端忽略证书验证。
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)pResponse
{
[_whiteList addObject:_FailedRequest];
[connection cancel];
[_webView loadRequest:_FailedRequest];
}
- (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])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
二,有些https请求需要带参数user-agent的参数,则可以通过一下方法获取。
1.设置属性
{
UIWebView *_web;
}
2.在viewdidload调用方法:[self createHttpRequest];
- (void)createHttpRequest {
_web = [[UIWebView alloc] init];
_web.delegate = self;
[_web loadRequest:[NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.eoe.cn"]]];
NSLog(@"%@", [self userAgentString]);
}
-(NSString *)userAgentString
{
while (self.userAgent == nil)
{
NSLog(@"%@", @"in while");
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return self.userAgent;
}
3.在shouldStartLoadWithRequest添加到request中去:
if (webView == _web) {
_userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
return NO;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。