3

今天遇到了一些奇怪的事情,也许有一个简单的解释。

如果我使用最初相对于作为子目录的基本 URL 构造的 NSURL 创建 NSURLRequest,则 NSURLRequest 会删除子目录。

这更容易用代码解释:

// create a base URL to work with
NSURL *baseUrl = [NSURL URLWithString:@"http://www.google.com/sub"];

// create a URL relative to the base URL
NSURL *url1 = [NSURL URLWithString:@"/foo/bar" relativeToURL:baseUrl];

// create a NSURLRequest using this first URL
NSURLRequest *mangled_req = [[NSURLRequest alloc] initWithURL:url1];
NSLog(@"url1: %@", url1);
NSLog(@"mangled_req: %@", mangled_req);

// now create another URL that puts the base URL together with the relative path
NSURL *url2 = [NSURL URLWithString:@"http://www.google.com/sub/foo/bar"];

// and create a NSURLRequest, which should be the same as above
NSURLRequest *correct_req = [[NSURLRequest alloc] initWithURL:url2];
NSLog(@"url2: %@", url2);
NSLog(@"correct_req: %@", correct_req);

输出证明了这一点:

2013-01-25 11:55:37.386 url1: /foo/bar -- http://www.google.com/sub
2013-01-25 11:55:37.408 mangled_req: <NSURLRequest http://www.google.com/foo/bar>
2013-01-25 11:55:37.409 url2: http://www.google.com/sub/foo/bar
2013-01-25 11:55:37.409 correct_req: <NSURLRequest http://www.google.com/sub/foo/bar>

请注意,“mangled_req”省略了 /sub。

这让我现在很烦,因为我正在使用 AFNetworking 并且想要在 localhost 之间切换以进行测试(它自然将我的 Web 应用程序放在一个子目录中)和远程服务器(它没有)。

当然有解决方法,但这对我来说似乎很奇怪,我一定做错了什么。

4

1 回答 1

8

NSURL行为正确,而您对 URL 工作方式的假设是错误的。以斜杠开头的“相对” URL/foo/bar始终表示相对于 host,而不是相对于现有路径。因此,如果我将/foo/bar相对 URL 添加到任何url scheme://host/path/1/2/3/whatever,我将始终返回scheme://host/foo/bar。路径上的前缀/意味着路径是绝对的。

当然,如果您将其修复为相对 URL 为foo/bar,您会发现仍然有问题,因为您的原始 URL 没有尾部斜杠。就像我在访问时单击指向的链接一样,如果somefile.html最后一个路径组件没有尾部斜杠,它将删除最后一个路径组件。http://host.com/foo/index.htmlhttp://host.com/foo/somefile.htmlhttp://host.com/foo/index.html/somefile.htmlNSURL

于 2013-01-25T01:15:46.707 回答