objective-c – 为什么NSURLConnection的currentRequest在重定向后没有指出正确的URL?

objective-c – 为什么NSURLConnection的currentRequest在重定向后没有指出正确的URL?,第1张

概述通过阅读NSURLConnection的文档,currentRequest方法应该反映加载时可能发生的任何重定向: As the connection performs the load, the request may change as a result of protocol canonicalization or due to following redirects. This metho 通过阅读NSURLConnection的文档,currentRequest方法应该反映加载时可能发生的任何重定向:

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.

解决方法 我只想把这个问题归结为苹果不得不混淆文档.我可以通过检查响应对象来获取最终请求URL,但对我来说,一个名为currentRequest的东西应该反映在重定向之后发出的最后一个请求. 总结

以上是内存溢出为你收集整理的objective-c – 为什么NSURLConnection的currentRequest在重定向后没有指出正确的URL?全部内容,希望文章能够帮你解决objective-c – 为什么NSURLConnection的currentRequest在重定向后没有指出正确的URL?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/999087.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存