gpt4 book ai didi

ios - NSHTTPURLResponse 变成 NSURLResponse

转载 作者:可可西里 更新时间:2023-11-01 05:47:14 35 4
gpt4 key购买 nike

我在 HTTP header 中使用 if-modified-since 来决定是否应该下载文件。应用程序已经过测试,一切正常,但现在当我询问我的 NSHTTPURLResponse 实例 response.statusCode[[response allHeaderFields] objectForKey:@"Last-Modified"]

它似乎只是 NSURLResponse。可能的原因是什么?

我读过 this topic但问题对我来说仍然不清楚。提前致谢!更新:一些代码:

        NSURL *url = [NSURL URLWithString:urlString];  
NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:url];
[myRequest setHTTPMethod:@"GET"];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

[myRequest addValue:[df stringFromDate:lastModifiedLocal] forHTTPHeaderField:@"If-Modified-Since"];

myRequest.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

NSHTTPURLResponse *response=nil;
NSError* error = nil;

NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error) {
NSLog(@"Error sending request: %@", [error localizedDescription]);
}
//As was advised
NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
//crash here
NSLog(@"%d", newResp.statusCode);

更新:代码错误 - myRequest 和 request 是不同的变量。问题解决了。

最佳答案

很可能是请求失败,没有生成响应对象。您可以按如下方式检查:

if (response) {
NSHTTPURLResponse* newResp = (NSHTTPURLResponse*)response;
NSLog(@"%d", newResp.statusCode);
}
else {
NSLog(@"No response received");
}

正如另一位评论者所建议的,您可能应该包含一个 NSError 对象,以便您可以更有效地检查错误:

NSHTTPURLResponse *response=nil;
NSError *error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: &error];

然后,您可以在检查响应之前检查错误:

if (error) {
NSLog(@"Error sending request: %@", [error localizedDescription]);
}

关于ios - NSHTTPURLResponse 变成 NSURLResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8892638/

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