iOS 拦截重定向302跳转的方法详解
程序员文章站
2024-02-13 09:26:05
一:前言
公司同事在做项目的时候遇到一个302地址跳转的问题,具体需求如下:
1、公司是做wifi覆盖的,需要在下载app后才能注册登录以后,自动连...
一:前言
公司同事在做项目的时候遇到一个302地址跳转的问题,具体需求如下:
1、公司是做wifi覆盖的,需要在下载app后才能注册登录以后,自动连接到外网
2、苹果底层不允许我们码农进行wifi的切换,所以公司考虑使用302重定向来实现,我通过2种方法来实现,nsurlconnetion和nsurlsession
二:nsurlconnetion方法
大概的思路:使用nsurlconnetion类的nsurlconnectiondatadelegate中的代理方法
复制代码 代码如下:
- (nullable nsurlrequest *)connection:(nsurlconnection *)connection willsendrequest:(nsurlrequest *)request redirectresponse:(nullable nsurlresponse *)response
都是码农,直接上代码吧
新建一个demo项目的话,记得把app transport security settings allow arbitrary loads设置为yes,否则无法请求网络
- (void)viewdidload { [super viewdidload]; nsurl *url = [nsurl urlwithstring:@"http://www.google.com"]; nsmutableurlrequest *quest = [nsmutableurlrequest requestwithurl:url]; quest.httpmethod = @"get"; nsurlconnection *connect = [nsurlconnection connectionwithrequest:quest delegate:self]; [connect start]; } - (nullable nsurlrequest *)connection:(nsurlconnection *)connection willsendrequest:(nsurlrequest *)request redirectresponse:(nullable nsurlresponse *)response{ nshttpurlresponse *urlresponse = (nshttpurlresponse *)response; nslog(@"%ld",urlresponse.statuscode); nslog(@"%@",urlresponse.allheaderfields); nsdictionary *dic = urlresponse.allheaderfields; nslog(@"%@",dic[@"location"]); return request; }
request 是你要发送的请求 urlresponse.statuscode 则是请求的状态码,302则是重定向 。urlresponse.allheaderfields中保存着http的头信息:
{ connection = close; "content-type" = "text/html"; date = "mon, 30 may 2016 04:00:49 gmt"; location = "http://118.244.233.137:6001/login/?gw_address=192.168.17.1&gw_port=2060&gw_id=ctzx_11&ip=192.168.17.24&mac=54:72:4f:30:c6:10&url=http%3a%2f%2fwww.google.com%2f"; server = "hughes technologies embedded server"; }
通过urlresponse.allheaderfields[@"location"]即可拿到302跳转的url地址.如果需要拦截掉这个url地址跳转,则可在代理方法中自定义一个request
- (void)urlsession:(nsurlsession *)session task:(nsurlsessiontask *)task willperformhttpredirection:(nshttpurlresponse *)response newrequest:(nsurlrequest *)request completionhandler:(void (^)(nsurlrequest * __nullable))completionhandler{ //block为nil,则直接拦截掉302的url地址,防止自动跳转 completionhandler(nil); }
则直接拦截掉302的url地址,防止自动跳转 completionhandler(nil);}
三:nsurlsession方法
- (void)viewdidload { [super viewdidload]; nsurl *url = [nsurl urlwithstring:@"http://www.google.com"]; nsmutableurlrequest *quest = [nsmutableurlrequest requestwithurl:url]; quest.httpmethod = @"get"; nsurlsessionconfiguration *config = [nsurlsessionconfiguration defaultsessionconfiguration]; config.requestcachepolicy = nsurlrequestreloadignoringlocalcachedata; nsurlsession *urlsession = [nsurlsession sessionwithconfiguration:config delegate:self delegatequeue:[nsoperationqueue currentqueue]]; nsurlsessiondatatask *task = [urlsession datataskwithrequest:quest completionhandler:^(nsdata * _nullable data, nsurlresponse * _nullable response, nserror * _nullable error) { nshttpurlresponse *urlresponse = (nshttpurlresponse *)response; nslog(@"%ld",urlresponse.statuscode); nslog(@"%@",urlresponse.allheaderfields); nsdictionary *dic = urlresponse.allheaderfields; nslog(@"%@",dic[@"location"]); }]; [task resume]; } - (void)urlsession:(nsurlsession *)session task:(nsurlsessiontask *)task willperformhttpredirection:(nshttpurlresponse *)response newrequest:(nsurlrequest *)request completionhandler:(void (^)(nsurlrequest * __nullable))completionhandler{ completionhandler(nil); }
使用方法和第一种方法一样
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Lucene实现索引和查询的实例讲解