在 iOS 中检索 HTTPResponse/HTTPRequest 状态代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/917932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve HTTPResponse/HTTPRequest status codes in iOS?
提问by cmcculloh
I need to check and evaluate the HTTP Status Codes in my iPhone app. I've got an NSURLRequest object and an NSURLConnection that successfully (I think) connect to the site:
我需要检查和评估我的 iPhone 应用程序中的 HTTP 状态代码。我有一个 NSURLRequest 对象和一个成功(我认为)连接到站点的 NSURLConnection:
// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData that will hold
// the received data
// receivedData is declared as a method instance elsewhere
receivedData=[[NSMutableData data] retain];
} else {
// inform the user that the download could not be made
}
I got this code here: https://web.archive.org/web/20100908130503/http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
我在这里得到了这个代码:https: //web.archive.org/web/20100908130503/http: //developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
But it doesn't (as far as I can see) say anything about accessing HTTP Status codes.
但它并没有(据我所知)说明访问 HTTP 状态代码的任何内容。
How to make a connection to a site and then check the HTTP Status Codes of that connection?
如何连接到站点,然后检查该连接的 HTTP 状态代码?
回答by Rob
This is how I do it:
这就是我的做法:
#pragma mark -
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int responseStatusCode = [httpResponse statusCode];
}
But I'm using an asynchronous NSURLConnection, so this may not help you. But I hope it does anyway!
但是我使用的是异步 NSURLConnection,所以这可能对您没有帮助。但我希望它无论如何!