在iOS中检索HTTPResponse/HTTPRequest状态代码? [英] Retrieve HTTPResponse/HTTPRequest status codes in iOS?
问题描述
我需要在我的iPhone应用程序中检查和评估HTTP状态代码.我有一个NSURLRequest对象和一个NSURLConnection,可以成功(我认为)连接到该站点:
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:
// 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
但据我所知,它并没有说出有关访问HTTP状态代码的任何信息.
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?
推荐答案
这是我的方法:
#pragma mark -
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int responseStatusCode = [httpResponse statusCode];
}
但是我正在使用异步NSURLConnection,因此这可能对您没有帮助.但我还是希望如此!
But I'm using an asynchronous NSURLConnection, so this may not help you. But I hope it does anyway!
这篇关于在iOS中检索HTTPResponse/HTTPRequest状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!