2009-04-01 16 views
9

मैं उद्देश्य-सी में एक प्रोग्राम लिख रहा हूं और मुझे वेब सर्वर पर वेब अनुरोध करने की आवश्यकता है, लेकिन असीमित रूप से और मैक पर काफी नया हूं, मैं विंडोज़ प्रौद्योगिकियों में बहुत अच्छा हूं, लेकिन मुझे यह जानने की ज़रूरत है कि अगर मैं एनएसओपरेशन का उपयोग करता हूं (10.5 में पेश किया गया है, तो मुझे लगता है कि यह 10.4 मैक में नहीं चलेगा?), या अगर इसे लागू किया गया था तो यह सिस्टम थ्रेडिंग का उपयोग करता है जो 10.4 पर उपलब्ध होगा?कुकीज़ के साथ उद्देश्य-सी असीमित वेब अनुरोध

या मुझे एक नया थ्रेड बनाना चाहिए और एक नया रनलोप बनाना चाहिए, कुकीज़ का उपयोग कैसे करें आदि, अगर कोई मुझे एक छोटा सा उदाहरण दे सकता है, तो यह बहुत मददगार होगा। मैं चाहता हूं कि यह नमूना मैक 10.4 पर भी चलने पर भी हो।

उत्तर

29

, एक वेबसाइट में लॉग इन करने SessionID कुकी भंडारण और भविष्य के अनुरोधों में फिर से सबमिट करने का एक पूरा वेब अनुप्रयोग उदाहरण करने के लिए NSURLRequest और NSHTTPCookies का उपयोग करने का एक अच्छा उदाहरण नहीं है।

NSURLConnection, NSHTTPCookie

logix812 द्वारा:

NSHTTPURLResponse * response; 
    NSError    * error; 
    NSMutableURLRequest * request; 
    request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://temp/gomh/authenticate.py?setCookie=1"] 
              cachePolicy:NSURLRequestReloadIgnoringCacheData 
             timeoutInterval:60] autorelease]; 

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]); 

    // If you want to get all of the cookies: 
    NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://temp"]]; 
    NSLog(@"How many Cookies: %d", all.count); 
    // Store the cookies: 
    // NSHTTPCookieStorage is a Singleton. 
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:all forURL:[NSURL URLWithString:@"http://temp"] mainDocumentURL:nil]; 

    // Now we can print all of the cookies we have: 
    for (NSHTTPCookie *cookie in all) 
     NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate); 


    // Now lets go back the other way. We want the server to know we have some cookies available: 
    // this availableCookies array is going to be the same as the 'all' array above. We could 
    // have just used the 'all' array, but this shows you how to get the cookies back from the singleton. 
    NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://temp"]]; 
    NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies]; 

    // we are just recycling the original request 
    [request setAllHTTPHeaderFields:headers]; 

    request.URL = [NSURL URLWithString:@"http://temp/gomh/authenticate.py"]; 
    error  = nil; 
    response = nil; 

    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSLog(@"The server saw:\n%@", [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease]); 
+0

बहुत बहुत धन्यवाद! – Hamy

+0

वह असीमित कैसे है ??? –

+1

आपके उत्तर के लिए धन्यवाद, लेकिन मेरे पास एक प्रश्न है, आपने उदाहरण के लिए अलग-अलग यूआरएल निर्दिष्ट किए हैं [यह] (http: //temp/gomh/authenticate.py? SetCookie = 1) यूआरएल और दूसरे के लिए एक http: // temp और इतने पर। मैं कुकीज के यूआरएल को कैसे जानता हूं, क्योंकि मेरे ऐप में जिस वेब सेवा का उपयोग कर रहा हूं, उसने यह जानकारी प्रदान नहीं की है? – Hamid

18

असीमित अनुरोधों के लिए, आपको NSURLConnection का उपयोग करने की आवश्यकता है।

कुकीज़ के लिए, NSHTTPCookie और NSHTTPCookieStorage देखें।

अद्यतन:

नीचे कोड मेरी अनुप्रयोगों में से एक से असली, काम कर कोड है। कक्षा इंटरफ़ेस में responseData को के रूप में परिभाषित किया गया है।

- (void)load { 
    NSURL *myURL = [NSURL URLWithString:@"http://stackoverflow.com/"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:myURL 
              cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
             timeoutInterval:60]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    responseData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [responseData release]; 
    [connection release]; 
    // Show error message 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // Use responseData 
    [responseData release]; 
    [connection release]; 
} 
+1

@Akash: क्या आपने NSURL कनेक्शन कनेक्शन का प्रयास किया हैथ्रूवेस्ट: प्रतिनिधि: विधि? और मुझे खेद है, लेकिन मुझे आपके प्रश्न में NSURLConnection या NSHTTPCookie का कोई उल्लेख नहीं है। मेरा विश्वास करो, जो कुछ भी आपको चाहिए वह मेरे द्वारा दिए गए लिंक में है। –

+0

मैंने पहले ही इसका इस्तेमाल किया था, यह काम नहीं करता था इसलिए मैंने सोचा कि एसिंक्रोनस अनुरोध करने का कोई और तरीका होना चाहिए क्योंकि ऐसा करने के लिए कोई नमूना कोड नहीं है। यहां कोड मैंने कोशिश की है .. http://stackoverflow.com/questions/706355/whats-wrong-on-following-urlconnection –

+0

@Akash: NSURLConnection सबसे आसान तरीका है। मैं नमूना कोड पोस्ट करूंगा। –

3

मैं इस तरह से कुकी लाने के लिए कर रहा हूँ:

NSArray* arr = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString: @"http://google.com" ]]; 

यह एक साथ ही अतुल्यकालिक अनुरोध के लिए ठीक काम करता है।

संबंधित मुद्दे