2009-05-27 42 views
28

我需要检查和评估我的iPhone应用程序中的HTTP状态代码。我有一个的NSURLRequest对象和NSURLConnection的是成功地(我认为)连接到站点:检索HTTPResponse/HTTPRequest状态码iPhone SDK?

// 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 
} 

我得到这个代码在这里:http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

但事实并非如此(只要我能看到)说任何关于访问HTTP状态代码。

任何人都有任何想法如何建立一个网站的连接,然后检查该连接的HTTP状态代码?

在此先感谢!

回答

76

这是我要做的事:

#pragma mark - 
    #pragma mark NSURLConnection Delegate Methods 
    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response { 
     NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; 
     int responseStatusCode = [httpResponse statusCode]; 
    } 

但我使用一个异步NSURLConnection的,所以这可能帮不了你。但我希望它无论如何!

+0

这确实实现了我所需要的。唯一的问题是,如果URL/IP Addy不存在,则不会收到响应,所以应用程序只会永远挂在那里。我目前正在寻找一种方法来解决这个问题......任何建议,将不胜感激...... – cmcculloh 2009-05-28 15:39:24