2009-05-27 78 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