NSURLRequest过BASIC验证、NTLM验证、以及UIWebView登录路由器

一、过路由器验证即UIWebView打开带BASIC验证的网页

1,需要引入的协议头文件

UIWebViewDelegate,NSURLConnectionDelegate,NSURLConnectionDataDelegate

2,_webView一定要设置loadRequest否则不会调用UIWebView的delegate方法,就无法做后续的认证工作

  _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    _webView.delegate = self;

    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];

    [self.view addSubview:_webView];

3,

//uiwebview发请求的时候就会调用此方法

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

 {

 NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);

 

 if (!_authed) {

 _authed = NO;

 /* pretty sure i'm leaking here, leave me alone... i just happen to leak sometimes */

[[NSURLConnection alloc] initWithRequest:request delegate:self];

return NO;

}

return YES;

}


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

{

    NSLog(@"got auth challange");

    

    if ([challenge previousFailureCount] == 0) {

        _authed = YES;

        /* SET YOUR credentials, i'm just hard coding them in, tweak as necessary */

        [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"admin" password:@"admin" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];

    } else {

        [[challenge sender] cancelAuthenticationChallenge:challenge];

    }

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

{

    NSLog(@"received response via nsurlconnection");

        /** THIS IS WHERE YOU SET MAKE THE NEW REQUEST TO UIWebView, which will use the new saved auth info **/

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.1"]];

    [_webView loadRequest:urlRequest];

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值