2

用户使用互联网连接被重定向到互联网提供商登录页面。

在他登录之前运行请求将为您提供来自提供者的 html 数据和来自您最初请求数据的地址的数据。

有没有办法检查您请求数据的数据地址是否与您获得响应的地址相同?

IE

有没有办法获取响应 URL?

4

1 回答 1

1

两者都有一个属性,但是比较它们NSURLRequest可能不会给你想要的结果。这里有一个类别会有所帮助:NSURLResponseURL-isEqual: NSURL

@interface NSURL (Equivalence)
- (BOOL)isEquivalent:(NSURL *)aURL;
@end

@implementation NSURL (Equivalence)
- (BOOL)isEquivalent:(NSURL *)aURL
{
    if ([self isEqual:aURL]) return YES;
    if ([[self scheme] caseInsensitiveCompare:[aURL scheme]] != NSOrderedSame) return NO;
    if ([[self host] caseInsensitiveCompare:[aURL host]] != NSOrderedSame) return NO;
    if ([[self path] compare:[aURL path]] != NSOrderedSame) return NO;
    if ([[self port] compare:[aURL port]] != NSOrderedSame) return NO;
    if ([[self query] compare:[aURL query]] != NSOrderedSame) return NO;
    return YES;
}
@end

导入该类别,您可以使用 AFNetworking 执行以下操作:

[self getPath:@"somePath" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if ([operation.request.URL isEquivalent:operation.response.URL]) {
        // they match
    } else {
        // there was a redirect
    }
} failure:nil];

编辑:我早就加入NSURL+Equivalence了我的标准工具箱,但功劳属于danh这个出色的 SO 答案

于 2013-02-21T02:48:32.960 回答