2009-04-01 38 views
9

Tôi đang viết một chương trình trong Mục tiêu-C và tôi cần thực hiện yêu cầu web tới máy chủ web, nhưng không đồng bộ và tôi khá mới trên mac, tôi rất giỏi công nghệ cửa sổ, nhưng Tôi cần phải biết rằng nếu tôi sử dụng NSOperation (giới thiệu trong 10.5, tôi giả định rằng nó sẽ không chạy trong 10.4 MAC?), Hoặc nếu nó đã được thực hiện như vậy mà nó sử dụng luồng hệ thống mà sẽ có sẵn trên 10.4? Hoặc tôi nên tạo một chủ đề mới và tạo một runloop mới, cũng làm thế nào để sử dụng cookie, vv, nếu có ai có thể cho tôi một ví dụ nhỏ, đó sẽ là một sự trợ giúp tuyệt vời. Tôi muốn mẫu này chạy trên mac 10.4 quá nếu có thể.Yêu cầu web không đồng bộ mục tiêu-C với cookie

Trả lời

29

Có một ví dụ điển hình của việc sử dụng NSURLRequest và NSHTTPCookies để làm một ví dụ ứng dụng web đầy đủ các cách đăng nhập vào một trang web, lưu trữ cookie SessionID và gửi lại nó trong các yêu cầu trong tương lai.

NSURLConnection, NSHTTPCookie

By 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

cảm ơn rất nhiều ở đây! – Hamy

+0

Làm thế nào là không đồng bộ? –

+1

Cảm ơn bạn đã trả lời, nhưng tôi có một câu hỏi, bạn đã chỉ định các URL khác nhau, ví dụ như URL đầu tiên bạn cung cấp [URL này] (http: //temp/gomh/authenticate.py? SetCookie = 1) và thứ hai một http: // temp và cứ thế. Làm cách nào để biết URL của cookie, vì dịch vụ web mà tôi đang sử dụng trong ứng dụng của tôi không cung cấp thông tin này? – Hamid

18

Đối với các yêu cầu không đồng bộ, bạn cần sử dụng NSURLConnection.

Đối với cookie, hãy xem NSHTTPCookieNSHTTPCookieStorage.

UPDATE:

Đoạn code dưới đây là có thật, mã làm việc kể từ một trong những ứng dụng của tôi. responseData được định nghĩa là NSMutableData* trong giao diện lớp học.

- (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: bạn đã thử kết nối NSURLConnection's connectionWithRequest: delegate: method? Và tôi xin lỗi nhưng tôi không thấy đề cập đến NSURLConnection hoặc NSHTTPCookie trong câu hỏi của bạn. Tin tôi đi, mọi thứ bạn cần đều nằm trong các liên kết tôi đã đưa ra. –

+0

Tôi đã sử dụng nó, nó không hoạt động vì vậy tôi nghĩ rằng phải có một số cách khác để làm yêu cầu không đồng bộ như không có bất kỳ mã mẫu để làm điều đó. Đây là mã tôi đã thử .. http://stackoverflow.com/questions/706355/whats-wrong-on-following-urlconnection –

+0

@Akash: NSURLConnection là cách dễ nhất. Tôi sẽ gửi mã mẫu. –

3

Tôi có thể lấy cookie trong theo cách như vậy:

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

Cái này hoạt động tốt cho yêu cầu không đồng bộ là tốt.

Các vấn đề liên quan