iOS https访问如何绕过无效证书权限访问
2015-06-08 17:16
519 查看
一,由于在项目中遇到一个网页访问会涉及多个证书的情况,每一种证书都只且仅需要绕一次权限,但是我们无法判断什么时候更换了证书。所以,网上的方法总是不能完美的解决问题。以下方法可以通用遇到的各种情况,同时非常感谢网友closure为我提供的这种方法。
@property(nonatomic,strong)UIWebView *webView;@property NSURLRequest *FailedRequest;@property NSURLResponse *response;@property NSURLConnection *connection;@property NSMutableSet *whiteList;@property NSMutableData *data;@property OSSpinLock lock;
- (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { _whiteList = [[NSMutableSet alloc] initWithCapacity:0]; _data = [[NSMutableData alloc] init]; } return self;}
- (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做信任处理。 [self _lock]; BOOL result = [_whiteList containsObject:[request URL]]; [self _unlock]; if (!result) { _FailedRequest = request; _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [_webView stopLoading]; } 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 *)response{ [self _lock]; [_whiteList addObject:[_FailedRequest URL]]; [self _unlock]; _FailedRequest = nil; _response = response;}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_data appendData:data];}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *str = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]; [_webView loadHTMLString:str baseURL:[_response URL]]; [_data setLength:0]; _response = nil;}- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];}//NSURLConnection的委托方法,- (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]; }
- (void)_lock { OSSpinLockLock(&_lock);}
- (void)_unlock { OSSpinLockUnlock(&_lock);}
二,有些https请求需要带参数user-agent的参数,则可以通过一下方法获取。
1.设置属性{ UIWebView *_web;}@property(nonatomic,strong)NSString *userAgent;
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; }
@property(nonatomic,strong)UIWebView *webView;@property NSURLRequest *FailedRequest;@property NSURLResponse *response;@property NSURLConnection *connection;@property NSMutableSet *whiteList;@property NSMutableData *data;@property OSSpinLock lock;
- (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { _whiteList = [[NSMutableSet alloc] initWithCapacity:0]; _data = [[NSMutableData alloc] init]; } return self;}
- (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做信任处理。 [self _lock]; BOOL result = [_whiteList containsObject:[request URL]]; [self _unlock]; if (!result) { _FailedRequest = request; _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [_webView stopLoading]; } 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 *)response{ [self _lock]; [_whiteList addObject:[_FailedRequest URL]]; [self _unlock]; _FailedRequest = nil; _response = response;}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_data appendData:data];}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *str = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]; [_webView loadHTMLString:str baseURL:[_response URL]]; [_data setLength:0]; _response = nil;}- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];}//NSURLConnection的委托方法,- (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]; }
- (void)_lock { OSSpinLockLock(&_lock);}
- (void)_unlock { OSSpinLockUnlock(&_lock);}
二,有些https请求需要带参数user-agent的参数,则可以通过一下方法获取。
1.设置属性{ UIWebView *_web;}@property(nonatomic,strong)NSString *userAgent;
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; }
相关文章推荐
- php重定向的三种方法分享
- 网站技巧之转发与重定向的区别
- php弹出对话框实现重定向代码
- PHP弹出提示框并跳转到新页面即重定向到新页面
- ASP.NET 重定向的几种方法小结
- Asp.Net 重定向必须要知道的一些资料
- php中修改浏览器的User-Agent来伪装你的浏览器和操作系统
- Linux base shell重定向详解
- linux URL的301重定向代码分析
- .htaccess重定向和url重写详细介绍
- 使用ISAPI_Rewrite做简单实用的301重定向
- Nginx下配置301重定向的正确方法例子
- Windows Powershell 管道和重定向
- shell 1>&2 2>&1 &>filename重定向的含义和区别
- php中get_meta_tags()、CURL与user-agent用法分析
- IIS重定向使用图文教程
- IIS服务器下做301永久重定向设置方法[可以传参][图文]
- shell常用重定向实例讲解
- JavaEE通过response实现请求重定向
- ASP.NET MVC3 实现全站重定向的简单方法