2009-05-27 13 views
28

मुझे अपने आईफोन ऐप में HTTP स्टेटस कोड की जांच और मूल्यांकन करने की आवश्यकता है।HTTPResponse/HTTPRequest स्टेटस कोड आईफोन एसडीके पुनर्प्राप्त करें?

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

मैं यहाँ इस कोड को मिला:: मैं एक NSURLRequest वस्तु और एक NSURLConnection कि सफलतापूर्वक (मुझे लगता है कि) साइट से कनेक्ट मिल गया है 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

यह मेरी ज़रूरत को पूरा करता था। एकमात्र समस्या यह है कि यदि यूआरएल/आईपी एडी मौजूद नहीं है, तो कोई प्रतिक्रिया प्राप्त नहीं होती है, इसलिए ऐप बस हमेशा के लिए लटकता है। मैं वर्तमान में इसे संबोधित करने के लिए एक रास्ता तलाश रहा हूं ... किसी भी सुझाव की सराहना की जाएगी ... – cmcculloh

+3

ऐप्पल से रीचबिलिटी कोड देखें। केवल अधिसूचना विधि का उपयोग करें (डिफ़ॉल्ट रूप से कोड में सक्षम नहीं है, init विधि में टिप्पणियां देखें) और हमेशा उस होस्ट को सेट करें जिसे आप जांचना चाहते हैं। http://stackoverflow.com/questions/477852/checking-for-internet-connectivity-in-objective-c –