Cocoa touch Cocoa Touch:如何检测下载服务器上丢失的文件

Cocoa touch Cocoa Touch:如何检测下载服务器上丢失的文件,cocoa-touch,http,networking,nsurlconnection,Cocoa Touch,Http,Networking,Nsurlconnection,我的iOS应用程序使用NSURLConnection类从服务器下载文档文件。我异步使用它 服务器上可能缺少文档(配置错误)。我希望NSURLConnection调用我的委托错误方法: - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 但事实并非如此。相反,它愉快地下载服务器返回的HTML错误404网页。我找到的唯一解决方法是检查nsurresponseMIMEtype属性,当它是

我的iOS应用程序使用
NSURLConnection
类从服务器下载文档文件。我异步使用它

服务器上可能缺少文档(配置错误)。我希望
NSURLConnection
调用我的委托错误方法:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
但事实并非如此。相反,它愉快地下载服务器返回的HTML错误404网页。我找到的唯一解决方法是检查
nsurresponse
MIMEtype
属性,当它是
text/html
时,我自己会失败。它工作得很好,但我不喜欢它,因为它使我以后无法支持html文档

如何检查服务器上是否确实存在请求的URL


谢谢

当您收到响应时(在
-connection:didReceiverResponse:
中),您可以查询响应。因为您使用的是HTTP,所以它将是一种
NSHTTPURLResponse
,尽管您可能需要对此进行测试。无论如何,它将告诉您一个
-statusCode
,可能是200、404或其他什么。

-(void)连接:(NSURLConnection*)连接didReceiverResponse:(nsurResponse*)响应
委托方法中,您可以请求响应以获取其错误代码

if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
        if (httpResponse.statusCode == 404) {
                ...
             //etc