使用 stringWithContentsOfURL 重定向后获取 url [英] get url after redirect with stringWithContentsOfURL

查看:64
本文介绍了使用 stringWithContentsOfURL 重定向后获取 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    NSString* HTML = [NSString stringWithContentsOfURL:url usedEncoding:&enc error:&error];

效果很好,但如果有重定向,我需要知道重定向页面的 url.

works great, but if there is a redirect I need to know the url of the redirected page.

有什么想法吗?

谢谢~~

推荐答案

您可能希望使用 NSURLConnection 并手动加载,而不是使用 stringWithContesOfURL 方法.然后你可以在你的连接上设置一个 URLConnectionDelegate.当您被重定向时,连接将在您的委托上调用此方法:

You will probably want to use NSURLConnection and load manually rather than using the stringWithContesOfURL method. Then you can set a URLConnectionDelegate on your connection. When you get redirected, the connection will call this method on your delegate:

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

请求参数提供您被重定向到的 URL.你可以检查它并用它做你想做的事.如果您想允许重定向继续,那么您应该从委托的方法中返回它.

The request argument provides the URL that you are being redirected to. You can inspect it and do what you want with it. If you want to allow the redirect to continue then you should return it from your delegate's method.

这篇关于使用 stringWithContentsOfURL 重定向后获取 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆