iOS NSURL 请求状态码总是 200
Posted
技术标签:
【中文标题】iOS NSURL 请求状态码总是 200【英文标题】:iOS NSURLRequests Status Code Always 200 【发布时间】:2013-05-16 21:34:13 【问题描述】:我们的要求包括检查对网络服务器上特定文件的 Internet 访问。此文件每 n 分钟检查一次。 NSURLRequests 从不调用 connection:didFailWithError 无论是否有互联网连接。 HTTP 状态始终为 200。Apple 的可访问性仅适用于域,而不适用于文件——因此它不符合要求。我怎样才能可靠地发现我是否可以每 n 分钟访问此文件?为什么http状态码不是真正的http状态码?
似乎可以回答此问题的其他 *** 问题不起作用: 1.How could connectionDidFinishLoading: run if no file is found on server? 2.Testing use of NSURLConnection with HTTP response error statuses
我尝试使用另一个带有完成块的队列,但这也没有用。
-(void) updateConnectionStatus
NSURL *url = [NSURL URLWithString:(NSString*)[appValues getValueForSettingsKey:@"company.project.test.pingURL"]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
//NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//__block __typeof__(self) _self = self;
connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
/*
[NSURLConnection
sendAsynchronousRequest:urlRequest queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode]; // ALWAYS 200 no matter what
NSString *pingFile = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",error); // NEVER has an error
//This doesn't even work because it remembers FOREVER the value once it gets it.
if ([@"Ping!" isEqualToString:pingFile])
dispatch_async(dispatch_get_main_queue(), ^
[_self companyConnection:YES];
);
else
dispatch_async(dispatch_get_main_queue(), ^
[_self companyConnection:NO];
);
];
*/
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
NSLog(@"ERROR: %@", error); // Never get here
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
NSHTTPURLResponse *aResponse = (NSHTTPURLResponse*)response;
NSLog(@"received a response: %ld",(long)[aResponse statusCode] );
if ([response respondsToSelector:@selector(statusCode)])
int statusCode = [((NSHTTPURLResponse *)response) statusCode];
// statusCode is always 200
if (statusCode >= 400)
[companyConnection cancel]; // stop connecting; no more delegate messages
NSDictionary *errorInfo
= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:
NSLocalizedString(@"Server returned status code %d",@""),
statusCode]
forKey:NSLocalizedDescriptionKey];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
NSLog(@"received data");
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSLog(@"Finished");
【问题讨论】:
您是否正在采取措施防止缓存收到的第一个响应? 如何防止缓存第一个响应?由于在每个方法的范围之外没有变量,因此在下一个请求通过时,这些变量似乎已经消失了。顺便说一句,我正在使用 ARC。 假设您在最新版本的 ios 上运行,NSURLCache 将自动缓存响应(取决于返回的标头)。 如果你实现willCacheResponse并返回nil
,那也应该禁用缓存。
Wain,如果您发布答案,我会将其标记为已接受。因为您的评论使我找到了解决方案。
【参考方案1】:
在构造 NSURLRequest 对象时尝试将 cachePolicy 设置为 NSURLRequestReloadIgnoringCacheData
【讨论】:
【参考方案2】:感谢 Wain 和 Rob 让我走上了正确的道路。保持缓存清除的一种方法是将此方法添加到您的 NSURLConnectionDelegate:
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
return nil;
【讨论】:
以上是关于iOS NSURL 请求状态码总是 200的主要内容,如果未能解决你的问题,请参考以下文章