无需对结果进行核对,以便:https://stackoverflow.com/questions/917932/retrieve-httprequest-status-codes-iphone-sdk>200 回复代码载于<代码>-(避免)联接:NSURLConnection*) 如果不存在,就应当获得404份。
<><>Edit>:
我愿补充说,<>Main上不存在的网页。 Wikipedia页面(不是移动电话)确实退回了正确的404个错误代码。 这可能会改变未来,如果改变任何内容,但两者都不会贬低内容,则可能完全可靠。 这里是我汇集的样本,以证明这一点。
NSURLRequest *exists = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/Qwerty"]];
//Redirects to Blivet
NSURLRequest *redirects = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/Poiuyt"]];
NSURLRequest *nonexistant = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/Jklfdsa"]];
NSHTTPURLResponse *resp_exists;
NSHTTPURLResponse *resp_redirects;
NSHTTPURLResponse *resp_nonexistant;
[NSURLConnection sendSynchronousRequest:exists returningResponse:&resp_exists error:NULL];
[NSURLConnection sendSynchronousRequest:redirects returningResponse:&resp_redirects error:NULL];
[NSURLConnection sendSynchronousRequest:nonexistant returningResponse:&resp_nonexistant error:NULL];
NSLog(@"
Exists: %d
Redirects: %d
Non Existant: %d",
[resp_exists statusCode],
[resp_redirects statusCode],
[resp_nonexistant statusCode] );
And here is the output
Exists: 200
Redirects: 200
Non Existant: 404
So if a page exists or automatically redirects to a page that does exist you will get a 200 error code, if it does not exist then you will get 404. If you would like to capture the redirect you will need implement -connection:willSendRequest:redirectResponse:
and act accordingly.
Note: This example code is synchronous for the sake of being compact. This is not ideal and production implementations should be sending asynchronous request and use the NSURLConectionDelegate
methods.