gpt4 book ai didi

objective-c - 为什么 NSURLConnection 的 currentRequest 在重定向后不指示正确的 URL?

转载 作者:可可西里 更新时间:2023-11-01 05:56:41 25 4
gpt4 key购买 nike

根据我对 NSURLConnection 文档的阅读,currentRequest 方法应该反射(reflect)加载时可能发生的任何重定向:

As the connection performs the load, the request may change as a result of protocol canonicalization or due to following redirects. This method is be used to retrieve the current value.

但是,当我检查 connectionDidFinishLoading: 中的 currentRequest 时,它始终具有原始请求的 URL,即使检查响应数据显示重定向已成功完成。 (请参阅下面的人为示例)

我的问题是:为什么 currentRequest 不返回实际的当前请求?我做错了什么还是这是预期的行为?如果这是 预期的行为,那么currentRequest 应该有什么用?

@interface AppDelegate : UIResponder <UIApplicationDelegate, NSURLConnectionDataDelegate>
@property (strong, nonatomic) NSURLConnection *connection;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *url = [[NSURL alloc] initWithString:@"http://jigsaw.w3.org/HTTP/300/301.html"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

return YES;
}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
NSLog(@"willSendRequest // request: %@ // currentRequest: %@", request, [connection currentRequest]);
return request;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"didFinishLoading // originalRequest: %@ // currentRequest: %@", [connection originalRequest], [connection currentRequest]);
}

@end

运行此示例会得到以下输出:

2012-12-18 15:18:44.949 ConnectionTest[12534:c07] willSendRequest // request: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html> // currentRequest: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html>
2012-12-18 15:18:44.954 ConnectionTest[12534:c07] willSendRequest // request: <NSURLRequest http://jigsaw.w3.org/HTTP/300/Overview.html> // currentRequest: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html>
2012-12-18 15:18:44.955 ConnectionTest[12534:c07] didFinishLoading // originalRequest: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html> // currentRequest: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html>

我的预期是最后一次调用 currentRequest 会显示以 Overview.html 结尾的页面 URL,但它仍然显示 301.html.

最佳答案

我只是想把这归咎于 Apple 不得不混淆文档。我可以通过检查响应对象获取最终请求 URL,但对我来说,名为 currentRequest 的东西应该反射(reflect)重定向后发出的最后一个请求。

关于objective-c - 为什么 NSURLConnection 的 currentRequest 在重定向后不指示正确的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13941084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com