connectionDidFinishLoading如何在服务器上找不到文件的情况下运行?
问题描述:
可能重复:
Testing use of NSURLConnection with HTTP response error statusesconnectionDidFinishLoading如何在服务器上找不到文件的情况下运行?
这是奇异;我有一个异步连接,像这样:
NSString *url=[NSString stringWithFormat:@"http://www.whatever.com/file"];
NSURL *url2=[NSURL URLWithString:url];
NSURLRequest *req=[[NSURLRequest alloc] initWithURL:url2];
NSURLConnection*con=[[NSURLConnection alloc] initWithRequest:req delegate:self];
[req release];
if(con){
NSMutableData *data=[[NSMutableData alloc] init];
self.receivedData=data;
[data release];
}
else {
UIAlertView*alert=[[UIAlertView alloc] initWithTitle:@"Error!" message:@"Unable to connect to server." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
然后我有一大堆的标准委托方法:
的NSLog的出现,结束时运行,即使该文件是不是在服务器上。为什么?我不希望它已经加载任何东西,而是导致错误和警报视图。
这些方法起初看起来很清楚,但是这里肯定有些事情我没有了解到异步连接。
答
它完成加载。它完成一个HTTP错误的事实就在这一点上。
您在didReceiveResponse中获得的NSHTTPResponse对象将具有.responseCode
属性,其中包含404。
看看[这个问题](http://stackoverflow.com/questions/697108/testing-use-of-nsurlconnection-with-http-response-error-statuses) – albertamg