首页 技术 正文
技术 2022年11月10日
0 收藏 956 点赞 4,200 浏览 3105 个字

就用一个很简单的例子

http://www.google.com谷歌的首页

都知道现在浏览器中打开google.com的话事实上会变成http://www.google.com.hk

网址被重定向了

如何在app中完成重定向呢

使用NSURLConnetion类的NSURLConnectionDataDelegate委托

NSURLConnectionDataDelegate委托中的这个方法

  1. – (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response

可以得到重定向以后的URL

看代码

  1. #import “ViewController.h”
  2. @interface ViewController ()
  3. @end
  4. @implementation ViewController
  5. – (void)viewDidLoad {
  6. [super viewDidLoad];
  7. NSURL *url = [NSURL URLWithString:@”http://www.google.com”];
  8. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  9. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  10. if (!connection) {
  11. NSLog(@”FAIL”);
  12. }
  13. }
  14. – (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
  15. NSLog(@”================================================”);
  16. NSLog(@”will send request\n%@”, [request URL]);
  17. NSLog(@”redirect response\n%@”, [response URL]);
  18. return request;
  19. }
  20. @end

以上省略了部分无关代码

解释一下代码

在viewDidLoad方法中进行了一次连接 正是谷歌的首页

再看看这个

  1. – (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response

这个方法在请求将要被发送出去之前会调用

返回值是一个NSURLRequest就是那个真正将要被发送的请求

第二个参数request就是被重定向处理过后的请求 在这里就可以拿到需要的URL

第三个参数response是一个将要触发重定向的请求

这里把request跟response中的URL打出来看一下

并直接返回request

运行看看结果

  1. ====will send request====
  2. http://www.google.com/
  3. ====redirect response====
  4. (null)
  5. ====will send request====
  6. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg
  7. ====redirect response====
  8. http://www.google.com/
  9. ====will send request====
  10. http://www.google.com.hk/
  11. ====redirect response====
  12. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg

可以看出进行了两次重定向

第一次

  1. ====will send request====
  2. http://www.google.com/
  3. ====redirect response====
  4. (null)

由于是第一次调用 没有进行重定向处理 所以redirect response是null

而想要被发送的请求就是www.google.com

第二次

  1. ====will send request====
  2. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg
  3. ====redirect response====
  4. http://www.google.com/

这个时候redirect response就不是null了 就是第一次中的那个request 也就是说这一次的重定向是由www.google.com这个URL引发的

而重定向的结果就是http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg

第三次

  1. ====will send request====
  2. http://www.google.com.hk/
  3. ====redirect response====
  4. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg

当然这个http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg

这个网址不是那个熟悉的网址

因为还要一次重定向

这次就跟上面一样了 那个很长的URL这次出现在了redirect response里 触发了这次重定向

结果可以看到

就是那个熟悉的www.google.com.hk

当获得想要的URL以后可以调用[connection cancel];方法来取消连接

并返回nil就好了

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297