码迷,mamicode.com
首页 > 移动开发 > 详细

iOS https访问如何绕过无效证书权限访问

时间:2015-06-08 19:45:24      阅读:1028      评论:0      收藏:0      [点我收藏+]

标签:https请求   重定向   无效证书   user-agent   

一,由于在项目中遇到一个网页访问会涉及多个证书的情况,每一种证书都只且仅需要绕一次权限,但是我们无法判断什么时候更换了证书。所以,网上的方法总是不能完美的解决问题。以下方法可以通用遇到的各种情况,同时非常感谢网友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;

    }




iOS https访问如何绕过无效证书权限访问

标签:https请求   重定向   无效证书   user-agent   

原文地址:http://843027677.blog.51cto.com/8104363/1659774

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!