2009-04-01 25 views
9

私はObjective-Cでプログラムを書いていますが、WebサーバーへのWebリクエストを作成する必要がありますが、非同期で、Macでかなり新しいです。 NSOperation(10.5で導入され、10.4 MACで動作しないと仮定していますか?)、または10.4で利用可能なシステムスレッドを利用するように実装されているかどうかを知る必要がありますか?Objective-C非同期Webリクエスト(Cookie付き)

新しいスレッドを作成して新しいrunloopを作成する必要があります。また、誰かが私に小さな例を与えることができれば、クッキーなどの使用方法も大いに役立ちます。可能であれば、このサンプルをMac 10.4でも実行したいと思います。

答えて

29

、ウェブサイトへのログインセッションIDクッキーを保存し、将来のリクエストでそれを再提出の完全なWebアプリケーションの例を行うにはNSURLRequestとNSHTTPCookiesを使用しての良い例があります。 logix812ことで

NSURLConnection, NSHTTPCookie

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

ありがとうございます、私は質問がありますが、最初に[this](http://temp/gomh/authenticate.py?setCookie = 1)のURLと2番目のURL 1つのhttp:// tempなどです。アプリで使用しているウェブサービスがこの情報を提供していないため、どのようにCookieのURLを知ることができますか? – Hamid

18

非同期要求の場合は、NSURLConnectionを使用する必要があります。

クッキーについては、NSHTTPCookieおよびNSHTTPCookieStorageを参照してください。

UPDATE:

次のコードは、私のアプリケーションの1からコードを作業、本物です。 responseDataは、クラスインタフェースでNSMutableData*と定義されています。

- (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:NSURLConnectionのconnectionWithRequest:delegate:メソッドを試しましたか?そして、申し訳ありませんが、あなたの質問に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" ]]; 

この1つだけでなく、非同期要求のために正常に動作します。

関連する問題