您的位置:首页 > 理论基础 > 计算机网络

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; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息